Resizing images
Posted on Sun 12 April 2020 in Programming
For the my other blog about models and hobby, I frequently need to resize a batch of images. Using ImageMagik is possible, but after a couple images it become boring and time consuming.
So, being stuck at home, I came up with a small python script which use the Python Image Library (in the Pillow fork) and the python-resize-image module.
The script is really simple and has the new size hardcoded to 1024x768, but for now it does what I need
# This project is licensed under the GNU General Public License v3.0
# Copyright (C) 2020 Gianluca Montecchi (gian@grys.it)
#
# A small script to resize all the images in the given directory base on the
# Python Image Library (Pillow) and the resizeimage module
import sys
import os
from PIL import Image
from resizeimage import resizeimage
def usage:
print("""
Usage:
blog_resize_images input_dir [output_dir]
input_dir -> directory where the original file are located
output_dir -> directory where to write the resized files
""")
if __name__ == "__main__":
if len(sys.argv) == 1:
usage()
if len(sys.argv) == 2:
INPUT_DIR = sys.argv[1]
if len(sys.argv) > 2:
usage()
files = os.listdir(INPUT_DIR)
for f in files:
print("Resizing file: ", f)
with open(f, "r+b") as FI:
with Image.open(f) as image:
cover = resizeimage.resize_contain(image, [1024,768])
cover = cover.convert("RGB")
cover.save(f, image.format)
The script is hosted on Gitlab in the myutils repo.