Welcome, guest | Sign In | My Account | Store | Cart

Image resizer/converter command-line utility.

Python, 101 lines
  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
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
# 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)

2 comments

FB36 (author) 5 years, 6 months ago  # | flag
# 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)
FB36 (author) 4 years, 11 months ago  # | flag
# Image Resizer [Batch Mode] w/o using PIL Image.resize
# and w/o using any image interpolation algorithm!
# FB36 - 20161210
from PIL import Image
import sys, os, glob
output = True
n = len(sys.argv)
if n != 4:
    usage = "USAGE:\n"
    usage += "[python] ImageResizer.py [Path]ImageFileName Width Height\n"
    usage += "ImageFileName can contain wildcards (* and/or ?)!\n"
    usage += "To preserve aspect ratio width or height must be 0!\n"
    print usage
    os._exit(1) # sys.exit()
imageFilePath = sys.argv[1]
width = int(sys.argv[2]) # in pixels
height = int(sys.argv[3]) # in pixels
fileList = glob.glob(imageFilePath)
for filePath in fileList:
    image = Image.open(filePath)
    (imgWidth, imgHeight) = image.size
    pixels = image.load() 
    w = width
    h = height
    # preserve aspect ratio
    if width > 0 and height == 0:
        h = int(width * imgHeight / imgWidth)
    elif width == 0 and height > 0:
        w = int(height * imgWidth / imgHeight)
    imageNew = Image.new("RGB", (w, h)) 
    pixelsNew = imageNew.load() 
    for ky in range(h):
        y = int(round((imgHeight - 1.0) * ky / (h - 1)))
        for kx in range(w):
            x = int(round((imgWidth - 1.0) * kx / (w - 1)))
            pixelsNew[kx, ky] = pixels[x, y]
    imageNew.save(filePath)
    if output:
        print filePath
        print "Old Size: " + str(imgWidth) + " " + str(imgHeight)
        print "New Size: " + str(w) + " " + str(h)
        print
Created by FB36 on Sat, 21 May 2016 (MIT)
Python recipes (4591)
FB36's recipes (148)

Required Modules

Other Information and Tasks