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,
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 = """
......@@ -26,20 +30,22 @@ path_str = """
"""
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"]
from PIL import Image
# 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="c:/Users/turdu001/wsm.rest/imageProcessing/testImages/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:
for folder in folders:
......@@ -48,11 +54,17 @@ def main(make_path, in_path, out_path):
if not os.path.exists(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:
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]
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 format in formats:
dir_path = os.path.join(out_path, folder, format)
......@@ -61,23 +73,39 @@ def main(make_path, in_path, out_path):
else:
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
if "per" in folder: # percent resize
per_value = int(folder.split("per")[0]) # extract percent value
#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}"
width = int((per_value / 100) * origin_width) # change to new width
width = 1 if width == 0 else width # if width =0 then width =1 (for small images)
print(f"running: {command}")
height = int((per_value / 100) * origin_height)
height = 1 if height == 0 else height # if height =0 then height =1 (for small images)
new_image = image.resize((width, height), Image.ANTIALIAS)
subprocess.call(command, shell=True)
if new_image.mode != 'RGB': # check if RGB conversion is required
new_image = new_image.convert('RGB')
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")
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