Я пытаюсь написать программу, в которой я обрежу, а затем поверну каждое изображение в папке. В настоящее время я могу сделать это для одного изображения, и я знаю, что могу вручную ввести их все. Но я хотел бы знать, как я могу l oop, где я могу запустить свой код несколько раз, так как есть около 500 изображений , Кроме того, я бегу Python3 на windows.
from PIL import Image
# Convert coordinate list into 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)
# If cropped area is vertical, rotate into horizontal position
if (y2 - y1) > (x2 - x1):
rotated_image = cropped_region.rotate(90, expand=1)
else:
rotated_image = cropped_region # Does nothing is image is horizontal
# Saving image to a new folder
rotated_image.save("C:\\Users\\Alex\Desktop\\Ps\\Output\\rotated.tiff", quality=95)