1. Installation
pip install pillow
Verify Installation:
from PIL import Image print(Image.__version__)
2. Import Pillow
from PIL import Image, ImageDraw, ImageFont, ImageFilter
3. Opening and Displaying an Image
img = Image.open('image.jpg')
img.show()
4. Saving an Image
img.save('output.jpg')
5. Image Properties
print(img.format) # Image format (JPEG, PNG) print(img.size) # (width, height) print(img.mode) # Color mode (RGB, L)
6. Resizing an Image
resized = img.resize((300, 200)) resized.show()
7. Cropping an Image
cropped = img.crop((50, 50, 200, 200)) # (left, top, right, bottom) cropped.show()
8. Rotating and Flipping
rotated = img.rotate(45) flipped = img.transpose(Image.FLIP_LEFT_RIGHT) rotated.show() flipped.show()
9. Converting to Grayscale
gray = img.convert('L')
gray.show()
10. Drawing on Images
draw = ImageDraw.Draw(img) draw.rectangle([50, 50, 150, 150], outline='red', width=5) draw.text((10, 10), "Hello!", fill='white') img.show()
11. Adding Filters
blurred = img.filter(ImageFilter.BLUR) sharpened = img.filter(ImageFilter.SHARPEN) blurred.show() sharpened.show()
12. Adding Text (Custom Fonts)
font = ImageFont.truetype("arial.ttf", 40)
draw = ImageDraw.Draw(img)
draw.text((50, 50), "Hello World!", font=font, fill='blue')
img.show()
13. Creating a New Image
new_img = Image.new('RGB', (400, 300), color='green')
new_img.show()
14. Merging Images (Pasting)
background = Image.new('RGB', (500, 500), color='white')
foreground = img.resize((200, 200))
background.paste(foreground, (150, 150))
background.show()
15. Applying Transparency (Alpha Channel)
img = img.convert("RGBA")
pixels = img.load()
for y in range(img.height):
for x in range(img.width):
r, g, b, a = pixels[x, y]
if r > 200 and g > 200:
pixels[x, y] = (r, g, b, 0) # Set transparent
img.show()
16. Thumbnail Generation
img.thumbnail((150, 150)) img.show()
17. Drawing Shapes
draw = ImageDraw.Draw(img) draw.ellipse([100, 100, 200, 200], outline='yellow', width=4) draw.line((0, 0, img.width, img.height), fill='blue', width=3) img.show()
18. Image Blending
img1 = Image.open('image1.jpg')
img2 = Image.open('image2.jpg')
blended = Image.blend(img1, img2, alpha=0.5)
blended.show()
19. Image Masking
mask = Image.new("L", img.size, 128) # 128 is 50% opacity
img.putalpha(mask)
img.show()
20. Combining Multiple Images (Grid Layout)
images = [Image.open('image1.jpg'), Image.open('image2.jpg')]
widths, heights = zip(*(i.size for i in images))
total_width = sum(widths)
max_height = max(heights)
combined = Image.new('RGB', (total_width, max_height))
x_offset = 0
for img in images:
combined.paste(img, (x_offset, 0))
x_offset += img.width
combined.show()
21. Image Metadata (EXIF)
exif_data = img._getexif()
for tag, value in exif_data.items():
print(tag, value)
22. Key Pillow Functions
| Function | Description |
|---|---|
| Image.open() | Opens an image file |
| Image.save() | Saves an image |
| resize() | Resizes an image |
| rotate() | Rotates an image |
| crop() | Crops an image |
| convert() | Converts image mode |
| filter() | Applies filters (BLUR, SHARPEN, etc.) |
| transpose() | Flips or rotates image |
| draw.rectangle() | Draws a rectangle |
| putalpha() | Adds transparency |
| thumbnail() | Creates a thumbnail |
| paste() | Pastes one image over another |
Example: Simple Image Pipeline (Resize, Filter, Text)
img = Image.open('image.jpg')
img = img.resize((300, 300))
img = img.filter(ImageFilter.CONTOUR)
draw = ImageDraw.Draw(img)
draw.text((20, 20), 'OpenCV Rocks!', fill='blue')
img.show()