python实现图像处理之PiL依赖库的案例应用详解
作者:Holidaylovesam 时间:2023-04-06 09:08:04
Python实现图像处理:PiL依赖库的应用
本文包含的练习题主要是PIL依赖库,即pillow相关的应用。
练习一:使用python给图片增加数字
实现思路:
使用PIL的Image.open导入图片。
获取图片的大小。
调用ImageDraw,在图片的指定位置写上数字。
#coding=utf-8
#Auther by Alice
#在图片的右上角增加一个数字
from PIL import Image,ImageFont,ImageDraw
image = Image.open('/Users/alice/Documents/Photo/IMG_8379.JPG')
#打开原图
wight, hight = image.size
text = "015"
color = (255,255,0)
fontsize = wight//10
font = ImageFont.truetype('Apple Symbols',fontsize)
#设定增加的数字的参数,数字内容、数字颜色和数字字号
draw = ImageDraw.Draw(image)
draw.text((fontsize*6,0), text, color, font)
image.save('/Users/alice/Documents/Photo/IMG_7997.JPG', 'jpeg')
#保存添加了数字之后的图片
实现前:
实现后:
修改其中两行代码字体和颜色如下后,
color = (105,200,45)
font = ImageFont.truetype('Palatino.ttc',fontsize)
则运行的结果为:
练习二:使用python将一个图片放大缩小
实现思路:
使用PIL,即Python图像标准依赖库。
使用open打开本地图片。
使用image.thumbnail放大缩小图片
#coding by alice
#coding=utf-8
from PIL import Image
im = Image.open('/Users/alice/Documents/Develop/PythonCode/test.JPG')
# 打开一个路径下的指定jpg图像文件
w,h = im.size
# 获得图像尺寸
im.thumbnail((w//10, h//10))
# 缩放到10%
im.save('/Users/alice/Documents/Develop/PythonCode/test2.JPG', 'jpeg')
# 把缩放后的图像用jpeg格式保存:
等同于代码:
#coding by alice
#coding=utf-8
from PIL import Image
image = Image.open('/Users/alice/Documents/Develop/PythonCode/test.JPG')
# 打开一个路径下的指定jpg图像文件
wight,hight = image.size
# 获得图像尺寸
image.thumbnail((weight//10, high//10))
# 缩放到10%
image.save('/Users/alice/Documents/Develop/PythonCode/test2.JPG', 'jpeg')
# 把缩放后的图像用jpg格式保存:
运行后的效果为:
练习三:使用python将一个图片实现模糊
实现思路:
使用PIL,即Python图像标准依赖库。
使用open打开本地图片。
使用image.thumbnail放大缩小图片
#coding by alice
#coding=utf-8
from PIL import Image
from PIL import ImageFilter
image = Image.open('/Users/alice/Documents/Develop/PythonCode/test.JPG')
# 打开一个路径下的jpg图像文件
image = image.filter(ImageFilter.BLUR)
# 应用模糊滤镜
image.save('/Users/alice/Documents/Develop/PythonCode/test3.JPG', 'jpeg')
#保存图片
运行后的结果为
如果是静物或者人脸,放大后看则模糊效果会更明显。
练习四:使用python获取一个图片的元素坐标
实现思路:
使用PIL,即Python图像标准依赖库。
使用open打开本地图片。
使用imshow显示图像
获取图片上点击光标,输出坐标
#coding by alice
#coding=utf-8
from PIL import Image
import matplotlib.pyplot as plt
image = Image.open('/Users/alice/Documents/Develop/PythonCode/test.JPG')
#打开所在位置及图像的名称
plt.figure('image')
#图像窗口名称
plt.imshow(image)
plt.show()
来源:https://blog.csdn.net/alice_tl/article/details/80866728
标签:python,图像处理,PiL依赖库
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
sql怎样显示出评论最多的文章?
2008-08-08 12:17:00
报错No module named numpy问题的解决办法
2023-09-20 12:02:07
![](https://img.aspxhome.com/file/2023/2/89412_0s.jpg)
python深度学习之多标签分类器及pytorch实现源码
2022-09-26 01:09:12
![](https://img.aspxhome.com/file/2023/1/97491_0s.jpg)
SpringBoot首页设置解析(推荐)
2021-11-03 05:43:00
![](https://img.aspxhome.com/file/2023/5/102935_0s.png)
python requests.get带header
2022-07-28 20:50:40
![](https://img.aspxhome.com/file/2023/1/97091_0s.jpg)
python实现线程池的方法
2023-03-10 14:08:06
常用CSS缩写语法总结章
2009-03-17 13:26:00
python 还原梯度下降算法实现一维线性回归
2023-10-09 21:53:42
![](https://img.aspxhome.com/file/2023/3/64633_0s.png)
Hexo已经看腻了,来手把手教你使用VuePress搭建个人博客
2024-04-26 17:37:08
![](https://img.aspxhome.com/file/2023/8/133458_0s.jpg)
gin 获取post请求的json body操作
2023-04-22 13:47:30
Python3中函数参数传递方式实例详解
2022-05-22 23:32:20
![](https://img.aspxhome.com/file/2023/6/135226_0s.png)
如何更改mysql命令下提示信息
2010-10-25 19:48:00
网站大改版=壮烈的死亡 ?
2009-04-03 14:09:00
![](https://img.aspxhome.com/file/UploadPic/20094/3/200903240046305586-33s.jpg)
JavaScript之解构赋值的理解
2024-04-10 10:44:07
![](https://img.aspxhome.com/file/2023/0/136980_0s.png)
利用PyInstaller将python程序.py转为.exe的方法详解
2021-07-09 16:41:51
Python 3.6 -win64环境安装PIL模块的教程
2021-10-30 11:02:31
![](https://img.aspxhome.com/file/2023/7/112397_0s.png)
SQL Server 索引和视图详解
2024-01-12 19:44:23
几个你不知道的技巧助你写出更优雅的vue.js代码
2024-05-13 09:14:39
![](https://img.aspxhome.com/file/2023/0/126420_0s.png)
解决PyTorch与CUDA版本不匹配的问题
2023-12-20 14:41:15
![](https://img.aspxhome.com/file/2023/6/92346_0s.jpg)
python 从文件夹抽取图片另存的方法
2022-08-26 13:00:55