处理图片是 Python 中常见的任务,尤其是在计算机视觉、图像处理和数据分析等领域。Python 提供了多个强大的库来处理图像,其中最常用的是 Pillow(PIL 的一个分支)和 OpenCV。
使用Pillow库
读取和显示图像:
from PIL import Image
import matplotlib.pyplot as plt
img = Image.open('image.jpg')
plt.imshow(img)
plt.show()
保存图像:
from PIL import Image
img = Image.open('image.jpg')
img.save('output_image.png', 'PNG')
调整图像大小:
from PIL import Image
img = Image.open('image.jpg')
resized_img = img.resize((800, 600))
resized_img.save('resized_image.jpg')
裁剪图像:
from PIL import Image
img = Image.open('image.jpg')
cropped_img = img.crop((50, 50, 300, 300)) # (left, upper, right, lower)
cropped_img.save('cropped_image.jpg')
旋转图像:
from PIL import Image
img = Image.open('image.jpg')
rotated_img = img.rotate(90)
rotated_img.save('rotated_image.jpg')
转换为灰度图像:
from PIL import Image
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", fnotallow=font)
img.save('text_image.jpg')
图像合成:
from PIL import Image
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')
图像锐化:
from PIL import Image, ImageFilter
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')
获取图像信息:
from PIL import Image
img = Image.open('image.jpg')
print(f"Format: {img.format}")
print(f"Size: {img.size}")
print(f"Mode: {img.mode}")
使用OpenCV库
读取和显示图像:
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()
保存图像:
import cv2
img = cv2.imread('image.jpg')
cv2.imwrite('output_image.png', img)
调整图像大小:
import cv2
img = cv2.imread('image.jpg')
resized_img = cv2.resize(img, (800, 600))
cv2.imwrite('resized_image.jpg', resized_img)
裁剪图像:
import cv2
img = cv2.imread('image.jpg')
cropped_img = img[50:300, 50:300]
cv2.imwrite('cropped_image.jpg', cropped_img)
旋转图像:
import cv2
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)
转换为灰度图像:
import cv2
img = cv2.imread('image.jpg', cv2.IMREAD_GRAYSCALE)
cv2.imwrite('gray_image.jpg', img)
边缘检测:
import cv2
img = cv2.imread('image.jpg', cv2.IMREAD_GRAYSCALE)
edges = cv2.Canny(img, 100, 200)
cv2.imwrite('edges_image.jpg', edges)
图像阈值处理:
import cv2
img = cv2.imread('image.jpg', cv2.IMREAD_GRAYSCALE)
_, thresh_img = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY)
cv2.imwrite('threshold_image.jpg', thresh_img)