-
Notifications
You must be signed in to change notification settings - Fork 1
/
rescale_images.py
62 lines (50 loc) · 1.78 KB
/
rescale_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
import os
import subprocess
import sys
import fnmatch
import tifffile
def get_img_info(root_dir):
file_list = {}
for root, dir, files in os.walk(root_dir):
if fnmatch.fnmatch(root,'*/RES(*'): #verifies that it's inside /RES*
for fn in fnmatch.filter(files,'*_*_*.tif'): #makes sure to fetch only full resolution images
if fn.find('res10') == 0: #skip res10 images
continue
file_name = os.path.join(root,fn)
file_list[fn] = root
print(fn)
return file_list
def convert(root_dir):
#create Image Magick tmp directory
home_dir = os.getcwd()
TMP_DIR = os.path.join(root_dir, "magick_tmp")
if not os.path.exists(TMP_DIR):
os.mkdir(TMP_DIR, 0777)
#export Image Magick env variables
os.environ['MAGICK_TMPDIR'] = TMP_DIR
os.environ['MAGICK_TMPDIR'] = '24Gb'
#get file info for all unprocessed tif files
image_files = get_img_info(root_dir)
#iterate over files in dict
for input_fn in image_files.keys():
output_fn = 'res10_' + input_fn
#enter image directory
os.chdir(image_files[input_fn])
if os.path.isfile(output_fn):
print('This image has already been resized.')
os.chdir(home_dir)
continue
#create file to be written to
temp = open(output_fn, 'w')
#do rescaling
subprocess.call(['convert', input_fn, '-resize', '10%', output_fn], env=dict(os.environ))
os.chdir(home_dir)
def main():
#check for user input
if (len(sys.argv) == 2):
root_dir = sys.argv[1]
convert(root_dir)
else:
print("Use: enter one directory")
if __name__ == "__main__":
main()