什么是PIL?
PIL(Python Imaging Library)是Python中最常用的图像处理库,它提供了丰富的图像处理功能,如打开、保存、调整大小、裁剪、旋转、滤镜等。PIL已经被Pillow库所取代,Pillow是PIL的一个分支,提供了更多的功能和更好的支持。
安装Pillow
基本操作
打开和显示图像
1 2 3 4 5 6 7 8 9 10 11 12
| from PIL import Image
img = Image.open('image.jpg')
img.show()
print(f"图像大小:{img.size}") print(f"图像模式:{img.mode}") print(f"图像格式:{img.format}")
|
保存图像
1 2 3 4 5 6 7 8
| from PIL import Image
img = Image.open('image.jpg')
img.save('image.png') img.save('image.bmp')
|
调整图像大小
1 2 3 4 5 6 7 8 9 10
| from PIL import Image
img = Image.open('image.jpg')
resized_img = img.resize((800, 600))
resized_img.save('resized_image.jpg')
|
裁剪图像
1 2 3 4 5 6 7 8 9 10
| from PIL import Image
img = Image.open('image.jpg')
cropped_img = img.crop((100, 100, 500, 400))
cropped_img.save('cropped_image.jpg')
|
旋转图像
1 2 3 4 5 6 7 8 9 10
| from PIL import Image
img = Image.open('image.jpg')
rotated_img = img.rotate(45)
rotated_img.save('rotated_image.jpg')
|
翻转图像
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| from PIL import Image
img = Image.open('image.jpg')
horizontal_flip = img.transpose(Image.FLIP_LEFT_RIGHT)
vertical_flip = img.transpose(Image.FLIP_TOP_BOTTOM)
horizontal_flip.save('horizontal_flip.jpg') vertical_flip.save('vertical_flip.jpg')
|
高级操作
应用滤镜
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| from PIL import Image, ImageFilter
img = Image.open('image.jpg')
blurred_img = img.filter(ImageFilter.BLUR)
edge_img = img.filter(ImageFilter.FIND_EDGES)
sharpened_img = img.filter(ImageFilter.SHARPEN)
blurred_img.save('blurred_image.jpg') edge_img.save('edge_image.jpg') sharpened_img.save('sharpened_image.jpg')
|
图像转换
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| from PIL import Image
img = Image.open('image.jpg')
gray_img = img.convert('L')
rgba_img = img.convert('RGBA')
gray_img.save('gray_image.jpg') rgba_img.save('rgba_image.png')
|
调整亮度和对比度
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| from PIL import Image, ImageEnhance
img = Image.open('image.jpg')
enhancer = ImageEnhance.Brightness(img) bright_img = enhancer.enhance(1.5)
enhancer = ImageEnhance.Contrast(img) contrast_img = enhancer.enhance(1.5)
enhancer = ImageEnhance.Color(img) saturation_img = enhancer.enhance(1.5)
bright_img.save('bright_image.jpg') contrast_img.save('contrast_image.jpg') saturation_img.save('saturation_image.jpg')
|
图像合成
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| from PIL import Image
background = Image.open('background.jpg')
foreground = Image.open('foreground.png')
foreground = foreground.resize((200, 200))
x = (background.width - foreground.width) // 2 y = (background.height - foreground.height) // 2
background.paste(foreground, (x, y), foreground)
background.save('composite_image.jpg')
|
实际应用示例
批量处理图像
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| from PIL import Image import os
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') or filename.endswith('.png'): img = Image.open(os.path.join(input_dir, filename)) resized_img = img.resize((800, 600)) gray_img = resized_img.convert('L') output_filename = os.path.splitext(filename)[0] + '_processed.jpg' gray_img.save(os.path.join(output_dir, output_filename))
print("批量处理完成")
|
创建缩略图
1 2 3 4 5 6 7 8 9 10
| from PIL import Image
img = Image.open('image.jpg')
img.thumbnail((300, 300))
img.save('thumbnail.jpg')
|
总结
Pillow库是Python中处理图像的强大工具,它提供了丰富的图像处理功能。通过本文的介绍,你应该已经掌握了它的基本用法和常见技巧。
无论是简单的图像打开、保存,还是复杂的图像处理和合成,Pillow库都能满足你的需求。记住,在处理图像时,要注意图像的格式、大小和质量,以确保处理后的图像符合你的要求。