Updated: Now uses os.listdir()
instead of glob.glob()
because of the need generate new file names from the original. Code now saves the resized images in same folder with its original file plus an added suffix.
Note that Image.open()
wants a file path passed to it, not an opened file.
import os
from PIL import Image
from resizeimage import resizeimage
img_folder = '/path/to/img_folder'
fileext = '.jpg'
suffix = '_RESIZED'
for img_filename in os.listdir(img_folder):
filename, ext = os.path.splitext(img_filename)
if ext == fileext:
print(filename, ext)
src_img_filepath = os.path.join(img_folder, img_filename)
dst_img_filepath = os.path.join(img_folder, filename+suffix, ext)
with Image.open(src_img_filepath) as image:
cover = resizeimage.resize_cover(image, [200, 100])
cover.save(dst_img_filepath, image.format)