包含编程资料、学习路线图、源代码、软件安装包等!【[点击这里]】!
今天我们将一起探索如何使用Python进行图像处理。Python 一种非常强大的编程语言,尤其适合图像处理。我们将使用Pillow
库来完成这些任务。如果你还没有安装这个库,可以通过下面的命令安装:
pip install pillow
1.导入图像
from PIL import Image
# 打开图片
img = Image.open('example.jpg')
# 显示图片
img.show()
2.获取图像尺寸
width, height = img.size
print(f"图片尺寸: {
width}x{
height}")
3.裁剪图像
# 裁剪参数:左上角坐标 (left, top) 和右下角坐标 (right, bottom)
cropped_img = img.crop((50, 50, 200, 200))
cropped_img.show()
4.图像缩放
resized_img = img.resize((400, 400))
resized_img.show()
5.图像旋转
rotated_img = img.rotate(90) # 逆时针旋转 90 度
rotated_img.show()
6. 图像翻转
# 水平翻转
flipped_horizontal = img.transpose(Image.FLIP_LEFT_RIGHT)
flipped_horizontal.show()
# 垂直翻转
flipped_vertical = img.transpose(Image.FLIP_TOP_BOTTOM)
flipped_vertical.show()
7.改变颜色模式
# 转换为灰度图
gray_img = img.convert('L')
gray_img.show()
# 转换为黑白图
bw_img = img.convert('1')
bw_img.show()
8.添加边框
bordered_img = ImageOps.expand(img, border=10, fill='red')
bordered_img.show()
9.图像合成
from PIL import ImageDraw
# 打开另一张图片
overlay_img = Image.open('overlay.png')
# 创建一个新的空白图像
result = Image.new('RGB', img.size)
# 将两张图片粘贴到新图像上
result.paste(img, (0, 0))
result.paste(overlay_img, (0, 0), overlay_img)
result.show()
10.图像拼接
# 创建一个新的空白图像
new_img = Image.new('RGB', (img.width * 2, img.height))
# 将两张图片拼接在一起
new_img.paste(img, (0, 0))
new_img.paste(img, (img.width, 0))
new_img.show()
11.图像滤镜
from PIL import ImageFilter
# 应用模糊滤镜
blurred_img = img.filter(ImageFilter.BLUR)
blurred_img.show()
# 应用锐化滤镜
sharpened_img = img.filter(ImageFilter.SHARPEN)
sharpened_img.show()
使用Python进行图像处理的11个基本操作
-
在第一部分中,我们介绍了使用 Python 和
Pillow
库进行图像处理的基本操作,包括导入图像、获取图像尺寸、裁剪图像、图像缩放、图像旋转、图像翻转、改变颜色模式、添加边框、图像合成、图像拼接以及应用滤镜。接下来,我们将通过一个实际场景的实战案例来进一步巩固这些知识点。
实战案例:制作个性化名片
假设我们要为一家公司设计一套个性化的名片模板。名片模板应该包含员工的照片、姓名、职位等信息。我们将使用前面学到的操作来实现这一目标。
1.准备素材
①一张背景图片(例如 background.jpg)。
②一张员工照片(例如 employee.jpg)。
③名字和职位信息(例如 “John Doe” 和 “Software Engineer”)。
2. 导入必要的库和图片
from PIL import Image, ImageDraw, ImageFont
# 打开背景图片
background = Image.open('background.jpg')
# 打开员工照片
photo = Image.open('employee.jpg')
3.调整员工照片大小并添加边框
# 调整照片大小
photo_resized = photo.resize((200, 200))
# 添加边框
photo_bordered = ImageOps.expand(photo_resized, border=10, fill='white')
4.在背景图片上粘贴照片
# 粘贴照片
background.paste(photo_bordered, (50, 50), photo_bordered)
5.添加文本信息
# 创建绘图对象
draw = ImageDraw.Draw(background)
# 设置字体
font = ImageFont.truetype('arial.ttf', 36)
# 绘制名字
name_text = 'John Doe'
draw.text((300, 100), name_text, font=font, fill=(0, 0, 0))
# 绘制职位
position_text = 'Software Engineer'
draw.text((300, 150), position_text, font=font, fill=(0, 0, 0))
6.应用滤镜效果
# 应用模糊滤镜
background_blurred = background.filter(ImageFilter.BLUR)
7.显示最终结果
background_blurred.show()
完整代码
下面是完整的代码示例:
from PIL import Image, ImageDraw, ImageFont, ImageOps, ImageFilter
# 打开背景图片
background = Image.open('background.jpg')
# 打开员工照片
photo = Image.open('employee.jpg')
# 调整照片大小
photo_resized = photo.resize((200, 200))
# 添加边框
photo_bordered = ImageOps.expand(photo_resized, border=10, fill='white')
# 粘贴照片
background.paste(photo_bordered, (50, 50), photo_bordered)
# 创建绘图对象
draw = ImageDraw.Draw(background)
# 设置字体
font = ImageFont.truetype('arial.ttf', 36)
# 绘制名字
name_text = 'John Doe'
draw.text((300, 100), name_text, font=font, fill=(0, 0, 0))
# 绘制职位
position_text = 'Software Engineer'
draw.text((300, 150), position_text, font=font, fill=(0, 0, 0))
# 应用模糊滤镜
background_blurred = background.filter(ImageFilter.BLUR)
# 显示最终结果
background_blurred.show()
分析
在这个实战案例中,我们综合运用了前面介绍的各种图像处理技术:
1.导入图片:使用 Image.open
方法导入背景图片和员工照片。
2.调整大小:使用 resize
方法调整照片的大小。
3.添加边框:使用 ImageOps.expand
方法为照片添加边框。
4.粘贴图片:使用 paste
方法将照片粘贴到背景图片上。
5.绘制文本:使用 ImageDraw
和 ImageFont
绘制名字和职位信息。
6.应用滤镜:使用 filter
方法为背景图片添加模糊滤镜效果。
通过这个案例,我们可以看到如何将多个图像处理技术结合起来,实现一个实用的功能。希望这个实战案例能够帮助大家更好地理解和掌握 Python 图像处理的基础知识。
总结
- 最后希望你编程学习上不急不躁,按照计划有条不紊推进,把任何一件事做到极致,都是不容易的,加油,努力!相信自己!
文末福利
- 最后这里免费分享给大家一份Python全套学习资料,希望能帮到那些不满现状,想提升自己却又没有方向的朋友,也可以和我一起来学习交流呀。
包含编程资料、学习路线图、源代码、软件安装包等!【[点击这里]】领取!
- ① Python所有方向的学习路线图,清楚各个方向要学什么东西
- ② 100多节Python课程视频,涵盖必备基础、爬虫和数据分析
- ③ 100多个Python实战案例,学习不再是只会理论
- ④ 华为出品独家Python漫画教程,手机也能学习