网站首页 > 技术文章 正文
注:本文所有代码均经过Python 3.7实际运行检验,保证其严谨性。
本文约2000字,阅读时间约为4分钟。
Pillow库的概述
Pillow库是Python最好的图像处理库,可能是使用频率最高的图像处理库。Python 2.X时,这个库名为PIL(即Python Image Library)。后来Python升级到3.X,PIL改名为Pillow。
Pillow功能非常强大,可以对图像做各种处理,包括缩放裁剪、旋转、滤镜、文字、调色板等等。
Pillow库的安装
Pillow库是Python的第三方库,其安装方法依然是在命令行界面输入conda install <第三方库名> 或 pip install <第三方库名>。
关于第三方库的安装,更多详情请移步至:Python零基础入门学习20:常用第三方库之PyInstaller库
Pillow库的操作
以下所有代码默认已有如下import操作:
from PIL import Image, ImageFilter
打开和浏览图像
打开图像
from PIL import Image
im = Image.open(<路径+图像名+文件格式>) # 如果是和源程序一个文件夹,直接输入图像名的字符串,比如"1.jpg"。
Pillow库能自动根据文件内容确定其格式,是jpg还是png等。
显示或保存图像
# 延续上面代码。
im.show() # 图像显示。im是上面代码中打开图像后形成的对象。
im.save(<文件名>) # 图像保存。
.thumbnail方法——缩略浏览图片
# 延续上面代码。
im.thumbnail(size, Image.ANTIALIAS)
第一个参数size是一个元组,比如(200, 400)表示宽200,高400的像素,为指定缩略图的尺寸大小。
第二个参数Image.ANTIALIAS是在内存中让缩略过程中对图像进行处理,让图像看起来更平滑,而不是出现锯齿等,处理后图片不会被拉伸。
PIL图像操作:模糊效果
.filter(ImageFilter.BLUR)——用于对图像进行模糊效果操作。
from PIL import Image, ImageFilter
# 打开一个jpg图像文件,注意是当前路径:
im = Image.open(<图像文件路径>)
# 应用模糊滤镜:
im2 = im.filter(ImageFilter.BLUR)
im2.save(<要保存的图像名称>, 'jpeg')
PIL图像操作:添加文字
from PIL import Image, ImageDraw, ImageFont
# 打开程序目录下的图片cat。
img = Image.open(<要添加文字的图像文件名>)
# 设置待添加文字的大小为200, 字体为宋体。
font = ImageFont.truetype('simsun.ttc', 100)
# 在img上创建可绘图对象draw。
draw = ImageDraw.Draw(img)
# 添加红色文字<要添加的文字>。
draw.text((100, 10), <要添加的文字>, (255, 0, 0), font=font)
# (100, 10)表示一个坐标,字体绘制的地方,第一个字左上角的位置。
# 保存照片。
img.save(<要保存的图像的文件名>, 'jpeg')
一个小程序:PIL生成验证码
from PIL import Image, ImageDraw, ImageFont, ImageFilter
import random
# 随机字母:
def rndChar():
return chr(random.randint(65, 90))
# 随机颜色1:
def rndColor1():
return (random.randint(64, 255), \
random.randint(64, 255), \
random.randint(64, 255))
# 随机颜色2:
def rndColor2():
return (random.randint(32, 255), \
random.randint(32, 255), \
random.randint(32, 255))
# 宽和高: 240 * 60。
width = 60 * 4
height = 60
image = Image.new('RGB', (width, height), (255, 255, 255))
# 创建Font对象。
font = ImageFont.truetype('simsun.ttc', 36)
# 创建Draw对象。
draw = ImageDraw.Draw(image)
# 填充每个像素。
for x in range(width):
for y in range(height):
draw.point((x, y), fill=rndColor1())
# 输出文字:
for t in range(4):
draw.text((60 * t + 10, 10), rndChar(), font=font, fill=rndColor2())
# 模糊:
#image = image.filter(ImageFilter.BLUR)
image.save('code.jpg', 'jpeg')
最后输出的图片code.jpg如下:
To be continued.
猜你喜欢
- 2025-06-18 python图片处理之图片切割(python切割图片中的人物)
- 2025-06-18 用python给图片批量打水印(用python批量处理图片)
- 2025-06-18 使用 Python 玩转图片(python教程图片)
- 2025-06-18 python视频播放器制作(python视频播放模块)
- 2025-06-18 一文教你——用Python发一个高逼格的朋友圈
- 2025-06-18 用python简单处理图片:打开\显示\保存图像
- 2025-06-18 Python提取图片中的文字信息,腾讯内部技术,一行代码搞定
- 2025-06-18 python实现截图功能(python怎么截图速度快)
- 2025-06-18 python如何给图片添加文字水印?(python添加图片要怎么弄)
- 2025-06-18 AI「自我复制」能力曝光,RepliBench警示:大模型正在学会伪造身份
- 268℃Python短文,Python中的嵌套条件语句(六)
- 267℃python笔记:for循环嵌套。end=""的作用,图形打印
- 265℃PythonNet:实现Python与.Net代码相互调用!
- 261℃Python实现字符串小写转大写并写入文件
- 260℃Python操作Sqlserver数据库(多库同时异步执行:增删改查)
- 120℃原来2025是完美的平方年,一起探索六种平方的算吧
- 100℃Python 和 JavaScript 终于联姻了!PythonMonkey 要火?
- 94℃Ollama v0.4.5-v0.4.7 更新集合:Ollama Python 库改进、新模型支持
- 最近发表
- 标签列表
-
- python中类 (31)
- python 迭代 (34)
- python 小写 (35)
- python怎么输出 (33)
- python 日志 (35)
- python语音 (31)
- python 工程师 (34)
- python3 安装 (31)
- python音乐 (31)
- 安卓 python (32)
- python 小游戏 (32)
- python 安卓 (31)
- python聚类 (34)
- python向量 (31)
- python大全 (31)
- python次方 (33)
- python桌面 (32)
- python总结 (34)
- python浏览器 (32)
- python 请求 (32)
- python 前端 (32)
- python验证码 (33)
- python 题目 (32)
- python 文件写 (33)
- python中的用法 (32)