Image Processing with Python: Pillow and OpenCV Guide

This guide demonstrates how to perform common image processing tasks in Python using the Pillow and OpenCV libraries, covering reading, displaying, saving, resizing, cropping, rotating, converting to grayscale, adding text, compositing, blurring, sharpening, enhancing, and extracting image metadata.

Test Development Learning Exchange
Test Development Learning Exchange
Test Development Learning Exchange
Image Processing with Python: Pillow and OpenCV Guide

Processing images is a common task in Python, especially in computer vision, image processing, and data analysis, and the most widely used libraries are Pillow and OpenCV.

Using the Pillow library

from PIL import Image
import matplotlib.pyplot as plt
img = Image.open('image.jpg')
plt.imshow(img)
plt.show()

img = Image.open('image.jpg')
img.save('output_image.png', 'PNG')

img = Image.open('image.jpg')
resized_img = img.resize((800, 600))
resized_img.save('resized_image.jpg')

img = Image.open('image.jpg')
cropped_img = img.crop((50, 50, 300, 300))  # (left, upper, right, lower)
cropped_img.save('cropped_image.jpg')

img = Image.open('image.jpg')
rotated_img = img.rotate(90)
rotated_img.save('rotated_image.jpg')

img = Image.open('image.jpg').convert('L')
img.save('gray_image.jpg')

from PIL import Image, ImageDraw, ImageFont
img = Image.open('image.jpg')
draw = ImageDraw.Draw(img)
font = ImageFont.truetype("arial.ttf", 36)
draw.text((50, 50), "Hello, World!", fill="white", font=font)
img.save('text_image.jpg')

background = Image.open('background.jpg')
foreground = Image.open('foreground.png').convert('RGBA')
background.paste(foreground, (0, 0), foreground)
background.save('composite_image.jpg')

from PIL import Image, ImageFilter
img = Image.open('image.jpg')
blurred_img = img.filter(ImageFilter.BLUR)
blurred_img.save('blurred_image.jpg')

img = Image.open('image.jpg')
sharpened_img = img.filter(ImageFilter.SHARPEN)
sharpened_img.save('sharpened_image.jpg')

from PIL import Image, ImageEnhance
img = Image.open('image.jpg')
enhancer = ImageEnhance.Contrast(img)
enhanced_img = enhancer.enhance(1.5)
enhanced_img.save('enhanced_image.jpg')

img = Image.open('image.jpg')
print(f"Format: {img.format}")
print(f"Size: {img.size}")
print(f"Mode: {img.mode}")

Using the OpenCV library

import cv2
import matplotlib.pyplot as plt
img = cv2.imread('image.jpg')
img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
plt.imshow(img_rgb)
plt.show()

img = cv2.imread('image.jpg')
cv2.imwrite('output_image.png', img)

img = cv2.imread('image.jpg')
resized_img = cv2.resize(img, (800, 600))
cv2.imwrite('resized_image.jpg', resized_img)

img = cv2.imread('image.jpg')
cropped_img = img[50:300, 50:300]
cv2.imwrite('cropped_image.jpg', cropped_img)

img = cv2.imread('image.jpg')
(h, w) = img.shape[:2]
center = (w // 2, h // 2)
M = cv2.getRotationMatrix2D(center, 90, 1.0)
rotated_img = cv2.warpAffine(img, M, (w, h))
cv2.imwrite('rotated_image.jpg', rotated_img)

img = cv2.imread('image.jpg', cv2.IMREAD_GRAYSCALE)
cv2.imwrite('gray_image.jpg', img)

edges = cv2.Canny(img, 100, 200)
cv2.imwrite('edges_image.jpg', edges)

_, thresh_img = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY)
cv2.imwrite('threshold_image.jpg', thresh_img)
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Computer VisionPythonOpenCV
Test Development Learning Exchange
Written by

Test Development Learning Exchange

Test Development Learning Exchange

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.