Significant overhaul of the script

parent ff8d786a
......@@ -3,32 +3,40 @@
from __future__ import division
import numpy as np
from argparse import ArgumentParser
from nibabel import load
from PIL import Image
def parse_args():
from argparse import ArgumentParser
DESCRIPTION = 'Generate a thumbnail from a NIfTI file'
parser = ArgumentParser(prog='nithumb',
description='Generate a thumbnail from a NIfTI file')
parser.add_argument('inputfile', help='Input NIfTI file')
parser.add_argument('thumbnail', help='Output thumbnail')
parsed = parser.parse_args()
parser = ArgumentParser(prog='nithumb', description=DESCRIPTION)
parser.add_argument('inputfile', help='input NIfTI file')
parser.add_argument('thumbnail', help='output thumbnail')
parser.add_argument('--size', '-s', type=int, nargs=2, default=(128, 128),
help='size of the thumbnail')
image = load(parsed.inputfile).get_data()
return parser.parse_args()
imin = np.min(image)
imax = np.max(image)
image = ((image - imin) / np.float(imax - imin)) * 255.
def main():
from nibabel import load
from PIL import Image
image = np.swapaxes(image, 0, 1)
parsed = parse_args()
image = Image.fromarray(image[:, :, image.shape[2]//2] if image.ndim == 3
else image)
# Load image with the correct orientation.
image = load(parsed.inputfile).get_data().swapaxes(0, 1)
image = image.resize([128, 128])
if image.mode != 'RGB':
image = image.convert('RGB')
image.save(parsed.thumbnail)
# Scale intensities to fit to 256 values.
image = ((image - image.min()) *
(255 / (image.max() - image.min()))).astype('uint8')
# Select the centre slice if 3D.
image = image[:, :, image.shape[2]//2] if image.ndim == 3 else image
# Generate and save the thumbnail.
Image.fromarray(image).resize(parsed.size).save(parsed.thumbnail)
if __name__ == '__main__':
main()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment