# Image Resizer/Converter [Batch Mode]
from PIL import Image, ImageFilter
import sys, os, glob
n = len(sys.argv)
if n < 2 or n > 6:
usage = "USAGE:\n"
usage += "[python] ImageResizerBatchMode.py"
usage += " [Path]InputImageFileName"
usage += " [Width] [Height] [Method] [Filter]\n"
usage += "[]: optional argument!\n"
usage += "Method: NEAREST(Default)/BILINEAR/BICUBIC/ANTIALIAS\n"
usage += "Filter: BLUR/CONTOUR/DETAIL/EDGE_ENHANCE/EDGE_ENHANCE_MORE"
usage += "/EMBOSS/FIND_EDGES/SMOOTH/SMOOTH_MORE/SHARPEN\n"
usage += "Use quotes if file paths/names contain spaces!\n"
usage += "InputImageFileName can contain wildcards (*/?)!"
print usage
os._exit(1) # sys.exit()
if n > 1:
inputImageFilePath = sys.argv[1]
width = 0
if n > 2:
width = int(sys.argv[2]) # in pixels
height = 0
if n > 3:
height = int(sys.argv[3]) # in pixels
method = ""
if n > 4:
method = sys.argv[4].upper()
imgFilter = ""
if n > 5:
imgFilter = sys.argv[5].upper()
fileList = glob.glob(inputImageFilePath)
for filePath in fileList:
image = Image.open(filePath)
(imgWidth, imgHeight) = image.size
if width == 0:
imWidth = imgWidth
else:
imWidth = width
if height == 0:
imHeight = imgHeight
else:
imHeight = height
if method == "NEAREST":
image = image.resize((imWidth, imHeight), Image.NEAREST)
elif method == "BILINEAR":
image = image.resize((imWidth, imHeight), Image.BILINEAR)
elif method == "BICUBIC":
image = image.resize((imWidth, imHeight), Image.BICUBIC)
elif method == "ANTIALIAS":
image = image.resize((imWidth, imHeight), Image.ANTIALIAS)
else:
image = image.resize((imWidth, imHeight))
if imgFilter == "BLUR":
image = image.filter(ImageFilter.BLUR)
if imgFilter == "CONTOUR":
image = image.filter(ImageFilter.CONTOUR)
if imgFilter == "DETAIL":
image = image.filter(ImageFilter.DETAIL)
if imgFilter == "EDGE_ENHANCE":
image = image.filter(ImageFilter.EDGE_ENHANCE)
if imgFilter == "EDGE_ENHANCE_MORE":
image = image.filter(ImageFilter.EDGE_ENHANCE_MORE)
if imgFilter == "EMBOSS":
image = image.filter(ImageFilter.EMBOSS)
if imgFilter == "FIND_EDGES":
image = image.filter(ImageFilter.FIND_EDGES)
if imgFilter == "SMOOTH":
image = image.filter(ImageFilter.SMOOTH)
if imgFilter == "SMOOTH_MORE":
image = image.filter(ImageFilter.SMOOTH_MORE)
if imgFilter == "SHARPEN":
image = image.filter(ImageFilter.SHARPEN)
image.save(filePath)
Snippet code written in Python that creates a batch mode for Image Resizer/Converter.
#python#batchMode #batch #imageResizer #imageConverter
#cesarnog
#python#batchMode #batch #imageResizer #imageConverter
#cesarnog
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.