# Image Resizer/Converter [Batch Mode]
# Supported Formats:
# http://effbot.org/imagingbook/formats.htm
# FB36 - 20160521
from PIL import Image, ImageFilter
import sys, os, glob
output = False
n = len(sys.argv)
if n < 3 or n > 7:
usage = "USAGE:\n"
usage += "[python] ImageResizer.py"
usage += " [Path]ImageFileName [Format=...]"
usage += " [Width=...] [Height=...] [Method=...] [Filter=...]\n"
usage += "[]: optional argument!\n"
usage += "Format: JPG/PNG/..."
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 += "ImageFileName can contain wildcards (*/?)!"
print usage
os._exit(1) # sys.exit()
imageFilePath = sys.argv[1]
imgFormat = ""
width = 0
height = 0
method = ""
imgFilter = ""
for i in range(n - 2):
args = sys.argv[i + 2].split("=")
if args[0] == "Format":
imgFormat = args[1].upper()
ext = imgFormat
if imgFormat == "JPG": imgFormat = "JPEG"
if imgFormat == "TIF": imgFormat = "TIFF"
if args[0] == "Width":
width = int(args[1]) # in pixels
if args[0] == "Height":
height = int(args[1]) # in pixels
if args[0] == "Method":
method = args[1].upper()
if args[0] == "Filter":
imgFilter = args[1].upper()
fileList = glob.glob(imageFilePath)
for filePath in fileList:
if output:
print filePath
image = Image.open(filePath)
(imgWidth, imgHeight) = image.size
if width == 0 and height == 0:
imWidth = imgWidth
imHeight = imgHeight
elif width > 0 and height > 0:
imWidth = width
imHeight = height
# preserve aspect ratio
elif width > 0 and height == 0:
imWidth = width
imHeight = int(imgHeight * width / imgWidth)
elif width == 0 and height > 0:
imWidth = int(imgWidth * height / imgHeight)
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)
if imgFormat != "":
filePathStr = os.path.normpath(os.path.splitext(filePath)[0] + "." + ext)
image.save(filePathStr, imgFormat)
else:
image.save(filePath)
Diff to Previous Revision
--- revision 1 2016-05-21 22:20:21
+++ revision 2 2016-05-22 16:45:52
@@ -1,68 +1,101 @@
-# Image Resizer/Converter
+# Image Resizer/Converter [Batch Mode]
# Supported Formats:
# http://effbot.org/imagingbook/formats.htm
# FB36 - 20160521
from PIL import Image, ImageFilter
-import sys, os
+import sys, os, glob
+output = False
n = len(sys.argv)
if n < 3 or n > 7:
usage = "USAGE:\n"
usage += "[python] ImageResizer.py"
- usage += " [Path]InputImageFileName [Path]OutputImageFileName"
- usage += " [Width] [Height] [Method] [Filter]\n"
+ usage += " [Path]ImageFileName [Format=...]"
+ usage += " [Width=...] [Height=...] [Method=...] [Filter=...]\n"
usage += "[]: optional argument!\n"
+ usage += "Format: JPG/PNG/..."
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!"
+ usage += "Use quotes if file paths/names contain spaces!\n"
+ usage += "ImageFileName can contain wildcards (*/?)!"
print usage
os._exit(1) # sys.exit()
-if n > 2:
- inputImageFilePath = sys.argv[1]
- outputImageFilePath = sys.argv[2]
- image = Image.open(inputImageFilePath)
- (width, height) = image.size
-if n > 3:
- width = int(sys.argv[3]) # in pixels
-if n > 4:
- height = int(sys.argv[4]) # in pixels
+imageFilePath = sys.argv[1]
+imgFormat = ""
+width = 0
+height = 0
method = ""
-if n > 5:
- method = sys.argv[5].upper()
imgFilter = ""
-if n > 6:
- imgFilter = sys.argv[6].upper()
+for i in range(n - 2):
+ args = sys.argv[i + 2].split("=")
+ if args[0] == "Format":
+ imgFormat = args[1].upper()
+ ext = imgFormat
+ if imgFormat == "JPG": imgFormat = "JPEG"
+ if imgFormat == "TIF": imgFormat = "TIFF"
+ if args[0] == "Width":
+ width = int(args[1]) # in pixels
+ if args[0] == "Height":
+ height = int(args[1]) # in pixels
+ if args[0] == "Method":
+ method = args[1].upper()
+ if args[0] == "Filter":
+ imgFilter = args[1].upper()
-if method == "NEAREST":
- image = image.resize((width, height), Image.NEAREST)
-elif method == "BILINEAR":
- image = image.resize((width, height), Image.BILINEAR)
-elif method == "BICUBIC":
- image = image.resize((width, height), Image.BICUBIC)
-elif method == "ANTIALIAS":
- image = image.resize((width, height), Image.ANTIALIAS)
-else:
- image = image.resize((width, height))
+fileList = glob.glob(imageFilePath)
+for filePath in fileList:
+ if output:
+ print filePath
+ image = Image.open(filePath)
+ (imgWidth, imgHeight) = image.size
+ if width == 0 and height == 0:
+ imWidth = imgWidth
+ imHeight = imgHeight
+ elif width > 0 and height > 0:
+ imWidth = width
+ imHeight = height
+ # preserve aspect ratio
+ elif width > 0 and height == 0:
+ imWidth = width
+ imHeight = int(imgHeight * width / imgWidth)
+ elif width == 0 and height > 0:
+ imWidth = int(imgWidth * height / imgHeight)
+ imHeight = height
-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)
+ 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))
-image.save(outputImageFilePath)
+ 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)
+
+ if imgFormat != "":
+ filePathStr = os.path.normpath(os.path.splitext(filePath)[0] + "." + ext)
+ image.save(filePathStr, imgFormat)
+ else:
+ image.save(filePath)