🐍 Python Image Manipulation Cheat Sheet

🐍 Python Image Manipulation Cheat Sheet #

A comprehensive guide to the most popular Python libraries for image manipulation: Pillow, OpenCV, and scikit-image.


🎯 Core Libraries #

Library Strengths Common Use Cases
Pillow User-friendly, great for general-purpose tasks. Web development, scripting, basic editing.
OpenCV High-performance, extensive computer vision algorithms. Real-time video analysis, object detection, facial recognition.
scikit-image Scientific imaging, tight integration with NumPy/SciPy. Research, medical imaging, advanced analysis.

πŸ› οΈ Installation #

# Pillow (PIL Fork)
pip install Pillow

# OpenCV
pip install opencv-python

# scikit-image
pip install scikit-image

# For NumPy integration (often a dependency, but good to have)
pip install numpy

πŸ–ΌοΈ Pillow (PIL Fork) #

A user-friendly library for opening, manipulating, and saving many different image file formats.

Basic Operations #

from PIL import Image, ImageFilter, ImageDraw, ImageFont

# Open an image
img = Image.open('image.jpg')

# Get basic info
print(img.format, img.size, img.mode)  # e.g., 'JPEG', (1920, 1080), 'RGB'

# Show the image (opens in default image viewer)
img.show()

# Save the image
img.save('new_image.png')

Manipulation #

# Resize
resized_img = img.resize((300, 200))

# Crop (box is a 4-tuple: left, upper, right, lower)
cropped_img = img.crop((100, 100, 400, 400))

# Rotate (counter-clockwise)
rotated_img = img.rotate(90, expand=True) # expand=True prevents cropping

# Flip
flipped_img = img.transpose(Image.FLIP_LEFT_RIGHT)

# Convert to grayscale
grayscale_img = img.convert('L')

Filters #

# Apply a built-in filter
blurred_img = img.filter(ImageFilter.BLUR)
sharpened_img = img.filter(ImageFilter.SHARPEN)
edges_img = img.filter(ImageFilter.FIND_EDGES)

Drawing #

# Create a drawing context
draw = ImageDraw.Draw(img)

# Draw a line
draw.line((0, 0, img.width, img.height), fill='red', width=5)

# Draw a rectangle
draw.rectangle((200, 100, 500, 400), outline='blue', width=3)

# Draw text
# font = ImageFont.truetype('arial.ttf', 36)
draw.text((10, 10), 'Hello, World!', fill='white') #, font=font)
img.show()

πŸ“Έ OpenCV-Python #

Optimized for real-time computer vision applications. Note: OpenCV loads images in BGR format, not RGB.

Basic Operations #

import cv2
import numpy as np

# Read an image
img_cv = cv2.imread('image.jpg')

# Get basic info
height, width, channels = img_cv.shape
print(f'Size: ({width}, {height}), Channels: {channels}')

# Display the image (requires a window manager)
# cv2.imshow('Image', img_cv)
# cv2.waitKey(0) # Wait for a key press to close
# cv2.destroyAllWindows()

# Save the image
cv2.imwrite('new_image_cv.png', img_cv)

Color Space Conversion #

# Convert BGR to Grayscale
gray_img_cv = cv2.cvtColor(img_cv, cv2.COLOR_BGR2GRAY)

# Convert BGR to RGB (for use with other libraries like Matplotlib)
rgb_img_cv = cv2.cvtColor(img_cv, cv2.COLOR_BGR2RGB)

Manipulation #

# Resize
resized_img_cv = cv2.resize(img_cv, (300, 200))

# Crop (using NumPy slicing)
cropped_img_cv = img_cv[100:400, 100:400] # [y:y+h, x:x+w]

# Rotate
(h, w) = img_cv.shape[:2]
center = (w // 2, h // 2)
matrix = cv2.getRotationMatrix2D(center, 45, 1.0) # (center, angle, scale)
rotated_img_cv = cv2.warpAffine(img_cv, matrix, (w, h))

πŸ”¬ scikit-image #

Focuses on scientific imaging and integrates tightly with the SciPy stack.

Basic Operations #

from skimage import io, color
from skimage.transform import resize, rotate

# Read an image (as a NumPy array)
img_sk = io.imread('image.jpg')

# Get basic info
print(img_sk.shape, img_sk.dtype) # e.g., (1080, 1920, 3), 'uint8'

# Display the image (requires Matplotlib)
# import matplotlib.pyplot as plt
# plt.imshow(img_sk)
# plt.show()

# Save the image
io.imsave('new_image_sk.png', img_sk)

Color & Manipulation #

# Convert to grayscale
gray_img_sk = color.rgb2gray(img_sk)

# Resize (returns a new image with the specified dimensions)
resized_img_sk = resize(img_sk, (200, 300), anti_aliasing=True)

# Rotate
rotated_img_sk = rotate(img_sk, 45, resize=True)

Advanced Features #

from skimage.feature import canny
from skimage.filters import sobel

# Edge detection
edges_canny = canny(gray_img_sk)

# Sobel filter for edge magnitude
edges_sobel = sobel(gray_img_sk)

# Displaying the results
# io.imshow(edges_canny)
# io.show()