Resize or crop all the images in a folder

Resize

How to resize all the images in a folder. When you run the script
>>> python shrink.py
you will be asked to input the folder where the images are and the width and height you want for the resized images.

To use PIL, you must install it like this:
pip install pillow

from PIL import Image
import os

print("Shrink images in the folder")
folder = input("folder: ")
w = int(input("width: "))
h = int(input("height: "))
for i in os.listdir(folder):
    file = f"{folder}\\{i}"
    im = Image.open(file)
    im = im.resize((w, h), Image.ANTIALIAS)
    im.save(file)

Crop

from PIL import Image
import os

print("Shrink images in the folder")
folder = input("folder: ")
wtop = int(input("wtop: "))
htop = int(input("htop: "))
wdown = int(input("wdown: "))
hdown = int(input("hdown: "))
for i in os.listdir(folder):
    file = f"{folder}\\{i}"
    im = Image.open(file)
    im = im.crop((wtop, htop, wdown, hdown))
    im.save(file)


Subscribe to the newsletter for updates
Tkinter templates
Avatar My youtube channel

Twitter: @pythonprogrammi - python_pygame

Videos

Speech recognition game

Pygame's Platform Game

Other Pygame's posts

Published by pythonprogramming

Started with basic on the spectrum, loved javascript in the 90ies and python in the 2000, now I am back with python, still making some javascript stuff when needed.