-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathresize_png_images.py
76 lines (57 loc) · 2.41 KB
/
resize_png_images.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Aug 7 19:19:00 2019
@author: Bastian
"""
import os, sys, glob
from PIL import Image
def image_padding(img, outsize):
""" Function to pad and scale image to desired size"""
old_size = img.size # old_size[0] is in (width, height) format
ratio = float(outsize)/max(old_size)
new_size = tuple([int(x*ratio) for x in old_size])
# use thumbnail() or resize() method to resize the input image
# thumbnail is a in-place operation
# im.thumbnail(new_size, Image.ANTIALIAS)
img = img.resize(new_size, resample=Image.BICUBIC)
# create a new image and paste the resized on it
new_img = Image.new("L", (outsize, outsize))
new_img.paste(img, ((outsize-new_size[0])//2,
(outsize-new_size[1])//2))
return new_img
def resize_images(pngpath, outPath, size):
if not os.path.exists(pngpath):
print('Filepath "'+pngpath+'" does not exist. Exiting')
sys.exit(2)
outPath = outPath + '/axis'
for axis in [0, 1, 2]:
# coronal, sag, axial directions
filepath = pngpath + '/axis' + str(axis) + '/'
out = outPath + str(axis)
if not os.path.exists(out):
os.makedirs(out)
for i in glob.glob(filepath + "*.png"):
'''
fd_img = open(i, 'r')
img = Image.open(fd_img)
img = resizeimage.resize_contain(img, [size, size])
img.save(outFile, img.format)
fd_img.close()
'''
filename = i.split('/')[-1] # get the filename with extension
filename_noext = filename.split('.png')[0] # remove png from name
slicenumber = i.split('_')[-1]
slicenumber = slicenumber.split('.png')[0]
origimg = Image.open(i)
outFile = out + '/' + filename_noext + "_" + \
"_slice_" + str(size) + "x" + str(size) + ".png"
img = image_padding(origimg, size)
img.save(outFile.format(slicenumber), img.format)
args = sys.argv[1:]
if len(args) < 3:
print("Expects at least 1 argument: input directory and (optional) output directory, \n EXAMPLE: python test.py ./myNifti.nii.gz ./output ")
inDir = args[0]
outDir = args[1]
sizeimg = int(args[2])
resize_images(inDir, outDir, sizeimg)