Python中如何用PiL依赖库对图像处理,操作是怎样
Admin 2022-06-07 群英技术资讯 649 次浏览
本文包含的练习题主要是PIL依赖库,即pillow相关的应用。
练习一:使用python给图片增加数字
实现思路:
#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将一个图片放大缩小
实现思路:
#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将一个图片实现模糊
实现思路:
#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获取一个图片的元素坐标
实现思路:
#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()
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
签到,都是规律性的操作,何尝不写一个程序加到Windows实现自动签到呢,本文就主要介绍了如何利用Python实现自动打卡签到的实践,具有一定的参考价值,感兴趣的可以了解一下
在我们使用Python中的Numpy和Pandas进行数据分析的时候,经常会遇到axis参数,本文就来介绍一下axis参数的具体使用,感兴趣的可以了解一下
这句话只要你学过python,你就很有可能在你的Python学习之旅的前30分钟就已经见过了,但是这句话具体是什么意思呢?
今天介绍一下Python中常用的正则表达式处理函数。Python的正则表达式主要有两种方法完成模式匹配:『搜索』和『匹配』 re match re mat
这篇文章主要介绍了pytorch实现简单全连接层的操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
成为群英会员,开启智能安全云计算之旅
立即注册Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008