188. Image Processing with PIL/Pillow

1. Opening and Displaying an Image

from PIL import Image

# Open an image
img = Image.open("example.jpg")
# Display the image
img.show()

2. Resizing an Image

from PIL import Image

# Open an image
img = Image.open("example.jpg")
# Resize the image to 200x200 pixels
resized_img = img.resize((200, 200))
# Save the resized image
resized_img.save("resized_example.jpg")

3. Cropping an Image

from PIL import Image

# Open an image
img = Image.open("example.jpg")
# Crop the image (left, upper, right, lower)
cropped_img = img.crop((50, 50, 200, 200))
# Save the cropped image
cropped_img.save("cropped_example.jpg")

4. Rotating an Image


5. Applying a Filter to an Image


6. Converting an Image to Grayscale


7. Adding Text to an Image


8. Creating a Thumbnail


9. Merging Two Images


10. Extracting Image Metadata


Last updated