python Pillow图像处理方法汇总
作者:巡山小妖N 时间:2021-09-08 18:02:45
这篇文章主要介绍了python Pillow图像处理方法汇总,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
Pillow中文文档:https://pillow-cn.readthedocs.io/zh_CN/latest/handbook/tutorial.html
安装:pip install pillow
操作图像:
#!/usr/bin/env python3
# _*_ coding utf-8 _*_
__author__ = 'nxz'
from PIL import Image, ImageFilter
from time import sleep
# 打开一个jpg图像文件
im = Image.open('test.jpg')
w, h = im.size #
print('图片的宽:%s,和高:%s' % (w, h))
# 图片缩放
im.thumbnail((w // 2, h // 2))
w, h = im.size
print(w, h)
# 缩放之后的图片重新保存
im.save('thumbnail.jpg', 'jpeg')
# 其他功能:切片、旋转、滤镜、输出文字、调色板
# 模糊效果
im2 = im.filter(ImageFilter.BLUR)
im2.save('blur.jpg','jpeg')
截屏:
from PIL import ImageGrab
from time import sleep
m = int(input("请输入想截屏多少次:"))
n = 1
while n <= m:
sleep(0.02)
im = ImageGrab.grab()
local = (r'%s.jpg' % (n))
im.save(local, 'jpeg')
n = n + 1
转换文件到JPEG:
'''
将指定路径下的图片后缀改为 “.jpg” 格式
'''
from PIL import Image
import os, sys
for infile in sys.argv[1:]:
f, e = os.path.splitext(infile)
outfile = f + '.jpg'
if infile != outfile:
try:
Image.open(infile).save(outfile)
except Exception as exc:
print(exc)
GIF动图:
"""
GIf动图
"""
from PIL import Image
im = Image.open('test.jpg')
images = []
images.append(Image.open('blur.png'))
images.append(Image.open('test.jpg'))
im.save('gif.gif', save_all=True, append_image=images, loop=1, duration=1, comment=b'aaaabbb')
几何变换:
#简单的集合变换
out = im.resize((128, 128))
#旋转图像
out = im.transpose(Image.FLIP_LEFT_RIGHT) #翻转
out = im.transpose(Image.FLIP_TOP_BOTTOM)
out = im.transpose(Image.ROTATE_90)
out = im.transpose(Image.ROTATE_180) #旋转180°
out = im.transpose(Image.ROTATE_270) #旋转270°
来源:https://www.cnblogs.com/nxzblogs/p/10663431.html
标签:python,pillow,图像,处理
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python控制浏览器自动下载歌词评论并生成词云图
2022-04-17 11:24:56
![](https://img.aspxhome.com/file/2023/9/83679_0s.png)
对python中的控制条件、循环和跳出详解
2022-03-08 00:41:44
解决python文件字符串转列表时遇到空行的问题
2021-06-27 09:18:25
Qzoneing主题视觉设计分享
2009-07-21 18:12:00
![](https://img.aspxhome.com/file/UploadPic/20097/21/1240_top-99s.jpg)
python numpy生成等差数列、等比数列的实例
2023-04-16 14:43:58
![](https://img.aspxhome.com/file/2023/6/95576_0s.jpg)
python用线性回归预测股票价格的实现代码
2023-01-24 02:14:31
![](https://img.aspxhome.com/file/2023/9/94129_0s.png)
Django restful framework生成API文档过程详解
2021-08-10 16:37:16
![](https://img.aspxhome.com/file/2023/5/76005_0s.png)
详解python中的lambda与sorted函数
2022-04-29 00:01:56
基于SQL Server的C/S数据库应用系统
2009-01-21 14:44:00
Python爬取求职网requests库和BeautifulSoup库使用详解
2021-12-29 09:07:49
![](https://img.aspxhome.com/file/2023/5/93465_0s.jpg)
各种SQL语句速查手册
2007-09-27 19:31:00
WEB手绘稿常用元素 之 箭头
2009-05-08 12:33:00
Python实现计算函数或程序执行时间
2022-03-22 14:28:28
![](https://img.aspxhome.com/file/2023/9/83759_0s.png)
asp常用的SQL命令操作
2007-09-29 12:21:00
asp截取指定英汉混合字符串_支持中文
2011-04-19 10:39:00
基于Python中isfile函数和isdir函数使用详解
2023-05-10 00:46:50
CSS样式表中继承关系的空格与不空格
2007-12-12 13:05:00
Python实现密钥密码(加解密)实例详解
2022-09-10 12:03:37
![](https://img.aspxhome.com/file/2023/4/67264_0s.jpg)
python中文件操作与异常的处理图文详解
2021-09-04 16:04:33
![](https://img.aspxhome.com/file/2023/2/78822_0s.jpg)
python制作小说爬虫实录
2023-09-30 17:39:25