Python Pillow: Your Image Journey Begins
DataScienceMPython tip:
To get started with image processing, use the Pillow library to open and display images.
from PIL import Image
# Make sure you have an image file (e.g., 'example.jpg') in the same directory
try:
img = Image.open('example.jpg')
print(f"Image format: {img.format}, size: {img.size}, mode: {img.mode}")
img.show() # This will open the image in your default image viewer
except FileNotFoundError:
print("Error: 'example.jpg' not found. Please create or provide an image file.")
except Exception as e:
print(f"An error occurred: {e}")Python tip:
Easily resize images to new dimensions using Pillow's resize() method.
from PIL import Image
try:
img = Image.open('example.jpg')
new_width = 300
new_height = 200
resized_img = img.resize((new_width, new_height))
resized_img.save('example_resized.jpg')
print(f"Image resized to {resized_img.size} and saved as 'example_resized.jpg'.")
except FileNotFoundError:
print("Error: 'example.jpg' not found. Cannot resize.")
except Exception as e:
print(f"An error occurred during resizing: {e}")Python tip:
Convert images to different color modes, like grayscale, with convert().
from PIL import Image
try:
img = Image.open('example.jpg')
grayscale_img = img.convert('L') # 'L' mode for grayscale
grayscale_img.save('example_grayscale.jpg')
print("Image converted to grayscale and saved as 'example_grayscale.jpg'.")
except FileNotFoundError:
print("Error: 'example.jpg' not found. Cannot convert.")
except Exception as e:
print(f"An error occurred during conversion: {e}")Python tip:
Crop a specific region from an image using the crop() method, specifying a bounding box (left, upper, right, lower).
from PIL import Image
try:
img = Image.open('example.jpg')
# Define the bounding box: (left, upper, right, lower)
# This example crops the top-left 100x100 pixels
cropped_img = img.crop((0, 0, 100, 100))
cropped_img.save('example_cropped.jpg')
print(f"Image cropped to {cropped_img.size} and saved as 'example_cropped.jpg'.")
except FileNotFoundError:
print("Error: 'example.jpg' not found. Cannot crop.")
except Exception as e:
print(f"An error occurred during cropping: {e}")Python tip:
Rotate an image by a specified angle using rotate().
from PIL import Image
try:
img = Image.open('example.jpg')
rotated_img = img.rotate(90, expand=True) # Rotate 90 degrees clockwise, expand to fit
rotated_img.save('example_rotated.jpg')
print("Image rotated 90 degrees and saved as 'example_rotated.jpg'.")
except FileNotFoundError:
print("Error: 'example.jpg' not found. Cannot rotate.")
except Exception as e:
print(f"An error occurred during rotation: {e}")Python tip:
Apply basic image filters from PIL.ImageFilter for quick visual effects.
from PIL import Image, ImageFilter
try:
img = Image.open('example.jpg')
blurred_img = img.filter(ImageFilter.BLUR)
blurred_img.save('example_blurred.jpg')
print("Image blurred and saved as 'example_blurred.jpg'.")
sharpened_img = img.filter(ImageFilter.SHARPEN)
sharpened_img.save('example_sharpened.jpg')
print("Image sharpened and saved as 'example_sharpened.jpg'.")
except FileNotFoundError:
print("Error: 'example.jpg' not found. Cannot apply filter.")
except Exception as e:
print(f"An error occurred during filtering: {e}")#PythonImageProcessing #PillowLibrary #ImageManipulation #PythonTips #ComputerVision #DigitalImaging #PythonForBeginners #PythonTricks #CodeQuality #LearnPython