Learn how to work with images in Python using the Pillow library. This exercise will teach you to:
- Open images from a given path
- Get image size and user input for cropping
- Crop an image using coordinates
- Preview the cropped image
- Optionally save the cropped image to disk
- Python 3.x
- Pillow library (
pip install pillow) - Basic understanding of functions, user input, and file paths
-
Get an image path from the user Use
input()to ask for the file path. -
Open the image
img = Image.open(path)
-
Display image size
print(img.size)
-
Ask the user for crop coordinates
left,top,right,bottomMake sure they are integers and within image dimensions.
-
Crop the image
cropped_img = img.crop((left, top, right, bottom))
-
Show the cropped image
cropped_img.show()
-
Optionally save the image
-
Ask the user if they want to save (
Y/N) -
Get folder path and filename from the user
-
Save using:
cropped_img.save(os.path.join(folder, filename))
-
-
Handle errors gracefully
- File not found
- Invalid input
- Any unexpected exceptions
Enter photo path: example.jpg
Image size: (1920, 1080)
Crop data:
Enter left pixel: 100
Enter top pixel: 100
Enter right pixel: 800
Enter bottom pixel: 600
Photo cropped! Details: (100, 100, 800, 600)
Do you want to save this photo? (Y/N): Y
Enter a path (folder) to save: ./cropped
Enter photo name (with extension, e.g., cropped.jpg): my_crop.jpg
Photo saved in ./cropped/my_crop.jpg- How to open and manipulate images with Pillow (
Image.open,.crop(),.show(),.save()) - How to work with file paths using the
osmodule - How to get and validate user input
- How to handle exceptions in Python for better error messages