Commit c0c5b34a authored by turdu001's avatar turdu001
Browse files

Moved code to PIL (Pillow) library

parent 38e6ed5b
""" """
Update: Ulan Turdukulov, 6th October 2020
1) Moved code to PIL library (Pillow): pip install Pillow
2) PIL doesn't have percent resize option - used width and height of the original image
Note, some formats (jpg) require RGB conversion
From a specific folder containing tiff original files will open the file and 10per, From a specific folder containing tiff original files will open the file and 10per,
20_200 1per 40_40050per, 80_800, original 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 = """ path_str = """
...@@ -26,20 +30,22 @@ path_str = """ ...@@ -26,20 +30,22 @@ path_str = """
""" """
import os import os
import glob import glob
import subprocess from PIL import Image
import shlex
import click
@click.command() # import shlex
@click.option('--make_path', is_flag = True, default = True ,help = "Will generate outputs path on destination folde") # import click
@click.option('--in_path',nargs=1, type=click.Path(exists=True), required=True, help = 'Path with original tiff images') # @click.command()
@click.option('--out_path', nargs=1, type=click.Path(exists=True), required=True, # @click.option('--make_path', is_flag = True, default = True ,help = "Will generate outputs path on destination folde")
help = f'Output path for transfomed images path structure: {path_str}') # @click.option('--in_path',nargs=1, type=click.Path(exists=True), required=True, help = 'Path with original tiff images')
def main(make_path, in_path, out_path): # @click.option('--out_path', nargs=1, type=click.Path(exists=True), required=True,
#def main(make_path = True, in_path = "/home/jesus/git/wsm.rest/imageProcessing/tmp3/original/tif", # help = f'Output path for transfomed images path structure: {path_str}')
# out_path="/home/jesus/git/wsm.rest/imageProcessing/final"): # def main(make_path, in_path, out_path):
formats = ["jpg","png","tif"] def main(make_path=True, in_path="c:/Users/turdu001/wsm.rest/imageProcessing/testImages/original",
folders = ["10per","1per","20_200","40_400","50per","80_800","original"] out_path="c:/Users/turdu001/wsm.rest/imageProcessing/testImages/final"):
formats = ["jpg", "png", "tif"]
folders = ["10per", "1per", "20_200", "40_400", "50per", "80_800", "original"]
if make_path: if make_path:
for folder in folders: for folder in folders:
...@@ -47,37 +53,59 @@ def main(make_path, in_path, out_path): ...@@ -47,37 +53,59 @@ def main(make_path, in_path, out_path):
dir_path = os.path.join(out_path, folder, format) dir_path = os.path.join(out_path, folder, format)
if not os.path.exists(dir_path): if not os.path.exists(dir_path):
os.makedirs(dir_path) os.makedirs(dir_path)
images = glob.glob(os.path.join(in_path,"*.tif")) images = glob.glob(os.path.join(in_path, "*.tif"))
for image in images: for image in images:
file_name = os.path.splitext(os.path.basename(image)) #('GB-001-def', '.tif') file_name = os.path.splitext(os.path.basename(image)) # ('GB-001-def', '.tif')
file_name = file_name[0] file_name = file_name[0]
image = Image.open(image)
# get original width and height of the image
origin_width = image.size[0]
origin_height = image.size[1]
for folder in folders: for folder in folders:
for format in formats: for format in formats:
dir_path = os.path.join(out_path, folder, format) dir_path = os.path.join(out_path, folder, format)
if folder == "original": if folder == "original":
format_name = "" format_name = ""
else: else:
format_name = f".{folder}" format_name = f".{folder}"
out_file_name = os.path.join(dir_path, file_name + format_name + f".{format}") #NG-006-def.50per.png 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: # percent resize
if "per" in folder: per_value = int(folder.split("per")[0]) # extract percent value
per_value = folder.split("per")[0]
command = f"convert -delete 1--1 {image} -resize {per_value}%! {out_file_name}" width = int((per_value / 100) * origin_width) # change to new width
if "_" in folder: width = 1 if width == 0 else width # if width =0 then width =1 (for small images)
res_size=folder.replace("_","x")
command = f"convert -delete 1--1 {image} -resize {res_size}! {out_file_name}" height = int((per_value / 100) * origin_height)
if "original" in folder: height = 1 if height == 0 else height # if height =0 then height =1 (for small images)
command = f"convert -delete 1--1 {image} {out_file_name}" new_image = image.resize((width, height), Image.ANTIALIAS)
print(f"running: {command}") if new_image.mode != 'RGB': # check if RGB conversion is required
new_image = new_image.convert('RGB')
subprocess.call(command, shell=True) new_image.save(out_file_name)
if "_" in folder: # pixel size resize
res_size = folder.split("_") # get new width & height
width = int(res_size[0])
height = int(res_size[1])
new_image = image.resize((width, height), Image.ANTIALIAS)
if new_image.mode != 'RGB': # check if RGB conversion is required
new_image = new_image.convert('RGB')
new_image.save(out_file_name)
if "original" in folder: # just convert and move original images
new_image = image.resize((origin_width, origin_height), Image.ANTIALIAS)
if new_image.mode != 'RGB': # check if RGB conversion is required
new_image = new_image.convert('RGB')
new_image.save(out_file_name)
print("all done") print("all done")
if __name__ == '__main__': if __name__ == '__main__':
main() main()
\ No newline at end of file
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