Applying anti-aliasing will reduce the jerkyness when reducing the photo. This is a Python code that reduces the photo files in a folder at once while maintaining the aspect ratio (aspect ratio), applies antialiasing, and saves them.
I reduced the photos of about 4000px to 300px and compared them. In the bird image with AA, the small sand on the ground is hard to see because the color is integrated with the ground.
#Install PIL
pip install PIL --allow-external PIL --allow-unverified PIL
Confirmed to work only in python2.7 environment of mac
resize.py
# -*- coding: utf-8 -*-
import commands
import Image
import re
#Image height pixels when shrinking
PHOTO_HEIGHT = 300
#Full path of the folder containing the image
BASE_DIR = "/Users/XXXXX/Desktop/Photos"
#Image regular expression name
PHOTO_REGEX = r"P.*.[jpg|JPG]"
#Image prefix after resizing
PHOTO_RESIZE_PREFIX = "r_"
def main():
#Get full image path
_cmd = "cd {} && ls".format(BASE_DIR)
l = commands.getoutput(_cmd)
l = l.split("\n")
l = [_l for _l in l if re.match(PHOTO_REGEX, _l)]
#Generate a folder for output
commands.getoutput("mkdir {}/output".format(BASE_DIR))
#Read an existing file in read mode
for _l in l:
before_path = '{}/{}'.format(BASE_DIR, _l)
filename = '{}{}'.format(PHOTO_RESIZE_PREFIX, _l)
after_path = '{}/output/{}'.format(BASE_DIR, filename)
resize(before_path, after_path, filename=_l) #Shrink
def resize(before, after, height=PHOTO_HEIGHT, filename="", aa_enable=True):
"""
Resize the image
:param str before:Original image file path
:param str after:Image file path after resizing
:param int height:Image height after resizing
:param bool aa_enable:Whether to enable antialiasing
:return:
"""
#Open image readonly
img = Image.open(before, 'r')
#Calculate image pixels after resizing
before_x, before_y = img.size[0], img.size[1]
x = int(round(float(height / float(before_y) * float(before_x))))
y = height
resize_img = img
if aa_enable:
#Shrink with antialiasing
resize_img.thumbnail((x, y), Image.ANTIALIAS)
else:
#Shrink without antialiasing
resize_img = resize_img.resize((x, y))
#Save the resized image
resize_img.save(after, 'jpeg', quality=100)
print "RESIZED!:{}[{}x{}] --> {}x{}".format(filename, before_x, before_y, x, y)
#Run
main()
Execution result
>>> python resize.py
RESIZED!:P1040673.jpg[4592x3448] --> 400x300
RESIZED!:P1050388.JPG[4592x3448] --> 400x300
RESIZED!:P1050389.JPG[4592x3448] --> 400x300
RESIZED!:P1050390.JPG[4592x3448] --> 400x300
RESIZED!:P1050391.JPG[4592x3448] --> 400x300
RESIZED!:P1050392.JPG[4592x3448] --> 400x300
RESIZED!:P1050393.JPG[4592x3448] --> 400x300
RESIZED!:P1050394.JPG[4592x3448] --> 400x300
Recommended Posts