Building a Python Image Editing Tool with Pillow, OpenCV, and NumPy
This guide demonstrates how to create a custom image editing tool in Python by leveraging the Pillow, OpenCV, and NumPy libraries, providing step‑by‑step code examples for opening, resizing, filtering, converting to grayscale, edge detection, rotation, channel manipulation, blurring, contour extraction, and color adjustment.
To build your own image editing tool, you can use several powerful Python image‑processing libraries. The following sections introduce Pillow, OpenCV, and NumPy, each accompanied by practical code snippets.
1. Pillow library – a popular library offering extensive image manipulation functions.
# Open and display an image
from PIL import Image
image = Image.open('image.jpg')
image.show()
# Resize an image
image = Image.open('image.jpg')
resized_image = image.resize((800, 600))
resized_image.save('resized_image.jpg')
# Apply a blur filter
from PIL import Image, ImageFilter
image = Image.open('image.jpg')
filtered_image = image.filter(ImageFilter.BLUR)
filtered_image.save('filtered_image.jpg')2. OpenCV library – a comprehensive computer‑vision library with many algorithms.
# Image grayscale conversion
import cv2
image = cv2.imread('image.jpg')
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
cv2.imshow('Gray Image', gray_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
# Edge detection using Canny
import cv2
image = cv2.imread('image.jpg')
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(gray_image, 100, 200)
cv2.imshow('Edges', edges)
cv2.waitKey(0)
cv2.destroyAllWindows()
# Image rotation
import cv2, numpy as np
image = cv2.imread('image.jpg')
height, width = image.shape[:2]
rotation_matrix = cv2.getRotationMatrix2D((width/2, height/2), 45, 1)
rotated_image = cv2.warpAffine(image, rotation_matrix, (width, height))
cv2.imshow('Rotated Image', rotated_image)
cv2.waitKey(0)
cv2.destroyAllWindows()3. NumPy library – widely used for scientific computing and image data handling.
# Pixel manipulation example
import cv2
import numpy as np
image = cv2.imread('image.jpg')
height, width = image.shape[:2]
black_image = np.zeros((height, width, 3), np.uint8)
black_image[100:300, 200:400] = image[100:300, 200:400]
cv2.imshow('Copied Image', black_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
# Channel split and merge
import cv2
image = cv2.imread('image.jpg')
b, g, r = cv2.split(image)
merged_image = cv2.merge((b, g, r))
cv2.imshow('Merged Image', merged_image)
cv2.waitKey(0)
cv2.destroyAllWindows()4. Image blur
from PIL import Image, ImageFilter
image = Image.open('image.jpg')
blurred_image = image.filter(ImageFilter.GaussianBlur(radius=2))
blurred_image.save('blurred_image.jpg')5. Contour extraction
import cv2
image = cv2.imread('image.jpg')
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(gray_image, 100, 200)
contours, _ = cv2.findContours(edges, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cv2.drawContours(image, contours, -1, (0, 255, 0), 2)
cv2.imshow('Contours', image)
cv2.waitKey(0)
cv2.destroyAllWindows()6. Color adjustment
from PIL import ImageEnhance
image = Image.open('image.jpg')
# Brightness enhancement
enhancer = ImageEnhance.Brightness(image)
brightened_image = enhancer.enhance(1.5)
# Contrast enhancement
enhancer = ImageEnhance.Contrast(brightened_image)
contrasted_image = enhancer.enhance(1.2)
contrasted_image.save('adjusted_image.jpg')These examples illustrate a variety of image‑processing operations such as blurring, contour detection, and color adjustments. By adapting and extending these snippets, you can create simple editors or tackle more complex computer‑vision tasks using Python’s rich ecosystem.
Test Development Learning Exchange
Test Development Learning Exchange
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.