|
| 1 | +#!/usr/local/bin/python3 |
| 2 | +""" |
| 3 | +Generates 3D renders of defaced images using FSLeyes. More info on FSLeyes can be found\ |
| 4 | + https://open.win.ox.ac.uk/pages/fsl/fsleyes/fsleyes/userdoc/install.html#install-from-conda-forge-recommended |
| 5 | +""" |
| 6 | +import argparse |
| 7 | +import subprocess |
| 8 | +from pathlib import Path |
| 9 | + |
| 10 | + |
| 11 | +def get_args(): |
| 12 | + parser = argparse.ArgumentParser(formatter_class=argparse.RawTextHelpFormatter, description=__doc__) |
| 13 | + |
| 14 | + parser.add_argument('-o', '--output', type=Path, action='store', dest='outdir', metavar='OUTPUT_DIR', |
| 15 | + default=Path('.'), help="Path to defacing outputs directory.") |
| 16 | + |
| 17 | + return parser.parse_args() |
| 18 | + |
| 19 | + |
| 20 | +def run_command(cmdstr): |
| 21 | + """Runs the given command str as shell subprocess. If logfile object is provided, then the stdout and stderr of the |
| 22 | + subprocess is written to the log file. |
| 23 | + :param str cmdstr: A shell command formatted as a string variable. |
| 24 | + :param io.TextIOWrapper logfile: optional, File object to log the stdout and stderr of the subprocess. |
| 25 | + """ |
| 26 | + p = subprocess.run(cmdstr, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, encoding='utf8', shell=True) |
| 27 | + print(p.stdout) |
| 28 | + |
| 29 | + |
| 30 | +def generate_3d_renders(defaced_img, render_outdir): |
| 31 | + rotations = [(45, 5, 10), (-45, 5, 10)] |
| 32 | + for idx, rot in enumerate(rotations): |
| 33 | + yaw, pitch, roll = rot[0], rot[1], rot[2] |
| 34 | + outfile = render_outdir.joinpath('defaced_render_99' + str(idx) + '.png') |
| 35 | + fsleyes_render_cmd = f"export TMP_DISPLAY=$DISPLAY; unset DISPLAY; fsleyes render --scene 3d -rot {yaw} {pitch} {roll} --outfile {outfile} {defaced_img} -dr 20 250 -in spline -bf 0.3 -r 100 -ns 500; export DISPLAY=$TMP_DISPLAY" |
| 36 | + print(fsleyes_render_cmd) |
| 37 | + run_command(fsleyes_render_cmd) |
| 38 | + print(f"Has the render been created? {outfile.exists()}") |
| 39 | + |
| 40 | + |
| 41 | +def main(): |
| 42 | + args = get_args() |
| 43 | + defaced_imgs = list(args.outdir.rglob('defaced.nii.gz')) |
| 44 | + for img in defaced_imgs: |
| 45 | + generate_3d_renders(img, img.parent) |
| 46 | + # print(defaced_imgs) |
| 47 | + |
| 48 | + |
| 49 | +if __name__ == "__main__": |
| 50 | + main() |
0 commit comments