Original2ResizeImages.py 2.94 KB
Newer Older
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
77
78
79
80
81
82
83
""" 
From a specific folder containing tiff original files will open the file and 10per, 
20_200 1per 40_40050per, 80_800, original

Notice: Please check the limits on:  /etc/ImageMagick-6/policy.xml Using env variables is not working

"""

path_str = """ 
├── 10per
│   ├── jpg
│   ├── png
│   └── tif
├── 1per
│   ├── jpg
│   ├── png
│   └── tif
├── 40_400
│   ├── jpg
│   ├── png
│   └── tif
└── original
    ├── jpg
    ├── png
    └── tif
"""
import os
import glob
import subprocess
import shlex
import click
@click.command()
@click.option('--make_path', is_flag = True, default = True ,help = "Will generate outputs path on destination folde")
@click.option('--in_path',nargs=1, type=click.Path(exists=True), required=True, help = 'Path with original tiff images')
@click.option('--out_path', nargs=1, type=click.Path(exists=True), required=True, 
              help = f'Output path for transfomed images path structure: {path_str}')
def main(make_path, in_path, out_path):
#def main(make_path = True, in_path = "/home/jesus/git/wsm.rest/imageProcessing/tmp3/original/tif",
#         out_path="/home/jesus/git/wsm.rest/imageProcessing/final"):
    
    formats = ["jpg","png","tif"]
    folders = ["10per","1per","20_200","40_400","50per","80_800","original"]

    if make_path:
        for folder in folders:
            for format in formats:
                dir_path = os.path.join(out_path, folder, format)
                if not os.path.exists(dir_path):
                    os.makedirs(dir_path)
    
    images = glob.glob(os.path.join(in_path,"*.tif"))
    
    for image in images:
        file_name = os.path.splitext(os.path.basename(image)) #('GB-001-def', '.tif')
        file_name = file_name[0]
        for folder in folders:
            for format in formats:
                dir_path = os.path.join(out_path, folder, format)
                if folder == "original":
                    format_name = ""
                else: 
                    format_name = f".{folder}"
                
                out_file_name = os.path.join(dir_path, file_name + format_name + f".{format}")  #NG-006-def.50per.png
                
                #Got problems with
                if "per" in folder:
                    per_value = folder.split("per")[0]
                    command = f"convert -delete 1--1 {image} -resize {per_value}% {out_file_name}"
                if "_" in folder:
                    res_size=folder.replace("_","x")
                    command = f"convert -delete 1--1 {image} -resize {res_size} {out_file_name}"
                if "original" in folder:
                    command = f"convert -delete 1--1 {image} {out_file_name}"

                print(f"running: {command}")    
                
                subprocess.call(command, shell=True)
    
    print("all done")
                    
if __name__ == '__main__':
    main()