Pillow Image 模块
Image 是 Pillow 库的核心模块,提供图像处理的基础功能。
以下是 Image 模块中最常用的方法及其功能描述:
方法 | 描述 | 示例 |
---|---|---|
Image.open(fp, mode='r') |
打开并识别图像文件 | img = Image.open('image.jpg') |
Image.new(mode, size, color=0) |
创建新图像 | new_img = Image.new('RGB', (500, 500), 'blue') |
Image.save(fp, format=None, **params) |
保存图像 | img.save('output.png') |
Image.show(title=None, command=None) |
显示图像 | img.show() |
Image.resize(size, resample=None, box=None) |
调整图像大小 | img.resize((200, 200)) |
Image.rotate(angle, resample=0, expand=0) |
旋转图像 | img.rotate(45) |
Image.crop(box=None) |
裁剪图像 | img.crop((100, 100, 400, 400)) |
Image.paste(im, box=None, mask=None) |
粘贴图像 | img.paste(logo, (50, 50)) |
Image.convert(mode, matrix=None, dither=None) |
转换图像模式 | img.convert('L') |
Image.filter(filter) |
应用滤镜 | img.filter(ImageFilter.BLUR) |
Image.copy() |
复制图像 | copy = img.copy() |
Image.thumbnail(size) |
创建缩略图 | img.thumbnail((100, 100)) |
Image.getpixel(xy) |
获取像素值 | pixel = img.getpixel((50, 50)) |
Image.putpixel(xy, value) |
设置像素值 | img.putpixel((50, 50), (255, 0, 0)) |
Image.split() |
分离通道 | r, g, b = img.split() |
Image.merge(mode, bands) |
合并通道 | Image.merge('RGB', (r, g, b)) |
3. 常用属性
除了方法外,Image 对象还有一些重要属性:
属性 | 描述 | 示例 |
---|---|---|
format |
图像格式 | print(img.format) |
size |
图像尺寸 (宽, 高) | print(img.size) |
width |
图像宽度 | print(img.width) |
height |
图像高度 | print(img.height) |
mode |
图像模式 | print(img.mode) |
info |
图像相关信息字典 | print(img.info) |
实际应用示例
基本图像操作
实例
from PIL import Image
# 打开图像
img = Image.open('example.jpg')
# 显示图像信息
print(f"格式: {img.format}, 大小: {img.size}, 模式: {img.mode}")
# 调整大小并保存
resized_img = img.resize((300, 300))
resized_img.save('resized_example.jpg')
# 转换为灰度图
gray_img = img.convert('L')
gray_img.save('gray_example.jpg')
# 打开图像
img = Image.open('example.jpg')
# 显示图像信息
print(f"格式: {img.format}, 大小: {img.size}, 模式: {img.mode}")
# 调整大小并保存
resized_img = img.resize((300, 300))
resized_img.save('resized_example.jpg')
# 转换为灰度图
gray_img = img.convert('L')
gray_img.save('gray_example.jpg')
图像处理
实例
from PIL import Image, ImageFilter
img = Image.open('example.jpg')
# 应用模糊滤镜
blurred = img.filter(ImageFilter.BLUR)
blurred.save('blurred.jpg')
# 应用轮廓滤镜
contour = img.filter(ImageFilter.CONTOUR)
contour.save('contour.jpg')
# 旋转图像
rotated = img.rotate(45, expand=True)
rotated.save('rotated.jpg')
img = Image.open('example.jpg')
# 应用模糊滤镜
blurred = img.filter(ImageFilter.BLUR)
blurred.save('blurred.jpg')
# 应用轮廓滤镜
contour = img.filter(ImageFilter.CONTOUR)
contour.save('contour.jpg')
# 旋转图像
rotated = img.rotate(45, expand=True)
rotated.save('rotated.jpg')
高级功能
图像合成
实例
from PIL import Image
# 打开两张图像
img1 = Image.open('background.jpg')
img2 = Image.open('foreground.png')
# 确保两张图像大小一致
img2 = img2.resize(img1.size)
# 合成图像 (假设 foreground.png 有透明通道)
composite = Image.alpha_composite(img1.convert('RGBA'), img2.convert('RGBA'))
composite.save('composite.png')
# 打开两张图像
img1 = Image.open('background.jpg')
img2 = Image.open('foreground.png')
# 确保两张图像大小一致
img2 = img2.resize(img1.size)
# 合成图像 (假设 foreground.png 有透明通道)
composite = Image.alpha_composite(img1.convert('RGBA'), img2.convert('RGBA'))
composite.save('composite.png')
批量处理
实例
import os
from PIL import Image
input_dir = 'input_images/'
output_dir = 'output_images/'
if not os.path.exists(output_dir):
os.makedirs(output_dir)
for filename in os.listdir(input_dir):
if filename.endswith('.jpg'):
img = Image.open(os.path.join(input_dir, filename))
# 转换为灰度并保存
gray_img = img.convert('L')
gray_img.save(os.path.join(output_dir, f"gray_{filename}"))
from PIL import Image
input_dir = 'input_images/'
output_dir = 'output_images/'
if not os.path.exists(output_dir):
os.makedirs(output_dir)
for filename in os.listdir(input_dir):
if filename.endswith('.jpg'):
img = Image.open(os.path.join(input_dir, filename))
# 转换为灰度并保存
gray_img = img.convert('L')
gray_img.save(os.path.join(output_dir, f"gray_{filename}"))
点我分享笔记