jeudi 13 février 2020

How to automatically rotate images if they are vertical

I recently starting coding a few days ago and I'm trying to write a program that crops images and then stacks them. I am currently still on step one. I want to make it where after the image is cropped, if is no already, rotate it to the horizontal position. Thank you for your time.

from PIL import Image

# Convert list in variables
print("Type in the coordinates for the upper left (x1,y1) and bottom right (x2,y2) points")
coordinates = list(map(int, input("Separate values with a space (x1 y1 x2 y2): ").strip().split()))[:4]
x1, y1, x2, y2 = coordinates
print("Generating image...")

# Accessing file from folder
im = Image.open("C:\\Users\\Alex\\Desktop\\Ps\\2.tiff")

# Cropping function
selected_region = (x1, y1, x2, y2)
cropped_region = im.crop(selected_region)
# Current line I am trying to add to rotate images
if y2-y1>x2-x1:
    cropped_region = cropped_region.transpose(Image.ROTATE_90)
    im.paste(cropped_region,selected_region)
cropped_region.show()

Also, hopefully this isn't asking for too much. So in this code, I am cropping one image, is there a way I can repeat this for all (about 500) images in the same folder (all labelled test1.tiff, test2.tiff, test3.tiff...)

Aucun commentaire:

Enregistrer un commentaire