模块 pillow图像处理
Pillow概况
PIL是Python的一种图像处理工具。
PIL支持大部分的图像格式,高效并强大。
核心库设计用来高速访问基于基于像素的数据存储,给这个通用的图像处理工具提供了坚实的基础。
一.读、写、显示、屏幕截图
from PIL import Image
# 从文件读
img1 = Image.open('wali.jpg')  # 读
# 从array数组读
img2 = Image.fromarray(image)
# todo 从内存读
img1.show()                     # 显示
img1.save('wali_test1.png')    # 写  : 写入时,可以更换格式
w, h = img1.size                # 利用内置方法获取图像的宽度高的
print(w, h, img1.size)          # 1920 1080 (1920, 1080)
显示 :是调用系统自带的图片查看器,以临时文件的方式查看

-写 :写的时候可以更改图像格式

屏幕截图
from PIL import ImageGrab
ImageGrab.grab().save(r'D:\wali\grab.jpg')
#     截图             保存图像

二.尺寸变换、旋转
1.尺寸变换 - 缩小- resize
from PIL import Image
img1 = Image.open('wali.jpg')       # 读
print(img1.size)                       #打印图片原始的尺寸
img1 = img1.resize((80, 60))          # 缩小尺寸
print(img1.size)                    #打印打印缩小后的图片尺寸
img1.save('wali_test2.png')  # 保存图片为'wali_test2.png'
#(1920, 1080)
#(80, 60)

2.尺寸变换 - 放大- resize
from PIL import Image
img1 = Image.open('wali_test2.png') #使用80*60 的瓦力作为放大的原图像
print(img1.size)
img1 = img1.resize((400, 300))
print(img1.size)
img1.save('wali_test3.png')
#(80, 60)
#(400, 300)

3.1尺寸变换 - 旋转90度- transpose
from PIL import Image
img1 = Image.open('800X600.png')
for x in range(7):
    img1.transpose(x).save('transpose%d.png'% x)
img1.close()    #关闭图像
# # transpose
# FLIP_LEFT_RIGHT = 0
# FLIP_TOP_BOTTOM = 1
# ROTATE_90 = 2
# ROTATE_180 = 3
# ROTATE_270 = 4
# TRANSPOSE = 5
# TRANSVERSE = 6

3.2尺寸变换 - 旋转(任意角度)- rotate
from PIL import Image
img1 = Image.open(r'D:\wali\400X300.jpg')
for x in range(30,361,30):
    img1.rotate(x).save(r'D:\wali\rotate%d.png'% x )

三.绘制图形
from PIL import Image, ImageDraw,ImageFont
Image1 = Image.new('RGB', (500, 400), (255, 255, 255))
draw =ImageDraw.Draw(Image1)
# 画直线
draw.line((20, 20, 150, 150), 'cyan', width=4)
draw.line((20, 150, 150, 20), 'red', width=2)
# 画矩形
draw.rectangle((170, 20, 300, 150), None , 'red', width=2)
# 画圆弧
draw.arc((340, 20, 470, 150), 0, 270, 'yellow', width=3)
# 画椭圆
draw.ellipse((20, 170, 150, 235), 'yellowgreen', 'wheat')
Image1.show()
四.绘制文字
1.英文
from PIL import Image, ImageDraw,ImageFont
Image1 = Image.new('RGB', (500, 400), (255, 255, 255))
draw =ImageDraw.Draw(Image1)
# 画文字_英文
draw.text((20, 250), u'Hello World', 'fuchsia')
draw.text((120, 250), u'Hello World', 'red')
Image1.show()
2.中文
from PIL import Image, ImageDraw,ImageFont
Image1 = Image.new('RGB', (500, 400), (255, 255, 255))
draw =ImageDraw.Draw(Image1)
# 画文字_中文
path_to_ttf = r'font/simfang.ttf'
font = ImageFont.truetype(path_to_ttf, size=25) # 设置字体
draw.text(xy=(20,280),text='Hello,中国!',fill='red',font=font)
Image1.show()

五.区域截图(抠图,覆盖)
from PIL import Image, ImageDraw
im = Image.open("t3.jpeg")
#   设置抠图区域
box = (350, 40, 507, 90)
#   从图片上抠下此区域
region = im.crop(box)
#   将此区域旋转180度
region = region.transpose(Image.ROTATE_180)
#   查看抠出来的区域
region.show()
#   将此区域粘回去
im.paste(region, box)
im.show()

六.滤镜
from PIL import Image
from PIL import ImageFilter
img1 = Image.open(r'D:\wali\z400X300.jpg')
img1.filter(ImageFilter.BLUR).save(r'D:\wali\1BLUR.png')
img1.filter(ImageFilter.CONTOUR).save(r'D:\wali\2CONTOUR.png')
img1.filter(ImageFilter.DETAIL).save(r'D:\wali\3DETAIL.png')
img1.filter(ImageFilter.EDGE_ENHANCE).save(r'D:\wali\4EDGE_ENHANCE.png')
img1.filter(ImageFilter.EDGE_ENHANCE_MORE).save(r'D:\wali\5EDGE_ENHANCE_MORE.png')
img1.filter(ImageFilter.EMBOSS).save(r'D:\wali\6EMBOSS.png')
img1.filter(ImageFilter.FIND_EDGES).save(r'D:\wali\7FIND_EDGES.png')
img1.filter(ImageFilter.SMOOTH).save(r'D:\wali\8SMOOTH.png')
img1.filter(ImageFilter.SMOOTH_MORE).save(r'D:\wali\9SMOOTH_MORE.png')
img1.filter(ImageFilter.SHARPEN).save(r'D:\wali\10SHARPEN.png')

七.透明叠加
from PIL import Image, ImageDraw,ImageFont
# 准备图像
img1 = Image.new('RGB', (500, 400), (255,255,255))
draw =ImageDraw.Draw(img1)
draw.line((20, 20, 150, 150), 'cyan', width=4)
draw.line((20, 150, 150, 20), 'red', width=2)
draw.rectangle((170, 20, 300, 150), 'black', 'red', width=2)
draw.arc((340, 20, 470, 150), 0, 270, 'yellow', width=3)
draw.ellipse((20, 170, 150, 235), 'yellowgreen', 'wheat')
draw.text((20, 250), u'Hello World', 'fuchsia')
draw.text((120, 250), u'Hello World', 'red')
path_to_ttf = r'font/simfang.ttf'
font = ImageFont.truetype(path_to_ttf, size=25) # 设置字体
draw.text(xy=(20,280),text='Hello,中国!',fill='red',font=font)
# a通道图像叠加
draw2 = ImageDraw.Draw(img1, 'RGBA')
draw2.rectangle((100, 100, 300, 300), fill=(0, 255, 0, 128))
img1.show()

模块 pillow图像处理的更多相关文章
- Pillow 模块~Python图像处理
		什么是验证码? 验证码(CAPTCHA)是“Completely Automated Public Turing test to tell Computers and Humans Apart”(全自 ... 
- 常用的第三方模块  Pillow    url
		Pillow PIL:Python Imaging Library,已经是Python平台事实上的图像处理标准库了.PIL功能非常强大,但API却非常简单易用. 由于PIL仅支持到Python 2.7 ... 
- python学习-Pillow图像处理
		Pillow中文文档:https://pillow-cn.readthedocs.io/zh_CN/latest/handbook/tutorial.html 安装:pip install pillo ... 
- python安装画图模块pillow
		步骤一: install pillow (注意导入是 import PIL ) 步骤二:如果pycharm中import选择不到,则需要在settings中导入下 ... 
- 解决Python图片处理模块pillow使用中出现的问题
		最近爬一个电影票房的网站(url:http://58921.com/alltime),上面总票房里面其实是一张图片,那么我需要把图片识别成文字,来获取票房数据. 我头脑里第一想到的解决方案就是要用 ... 
- Pillow模块图片生成
		0825自我总结 Pillow模块图片生成 一.模块安装 pip3 install pillow 二.模块的载入 import PIL 三.django结合img标签生成图片 img.html < ... 
- Python图像处理之验证码识别
		在上一篇博客Python图像处理之图片文字识别(OCR)中我们介绍了在Python中如何利用Tesseract软件来识别图片中的英文与中文,本文将具体介绍如何在Python中利用Tesseract ... 
- python模块统计
		.处理日期和时间 datetime/time/pytz/dateutil/calendar 注:calendar有很广泛的方法用来处理年历和月历,例如打印某月的月历 .处理字符串 re .处理字符集编 ... 
- Pycharm安装opencv与 无法安装PIL以及安装Pillow之后依然报错的解决办法
		Pycharm 安装opencv pycharm里自带了很方便的安装第三方库的方法,不需要自己去下载opencv包再pip install 在pycharm的File/Settings/Project ... 
随机推荐
- 7-3 jmu-python-回文数判断(5位数字) (10 分)
			本题目要求输入一个5位自然数n,如果n的各位数字反向排列所得的自然数与n相等,则输出‘yes’,否则输出‘no’. 输入格式: 13531 输出格式: yes 输入样例1: 13531 输出样例1: ... 
- CSS单位计算总结
			CSS单位总结 公共部分css body { background-color: #000; color: skyblue; margin: 0; padding: 0; } body>div& ... 
- VUE一 基础语法
			一.模板语法 二.Class和Style绑定 三.条件渲染 四.vue事件处理器 五.vue组件 
- swoole websocket_server 聊天室--群聊
			centos7 php7.2 swoole4.3 nginx1.8 websocket_server 代码 <?php $server = new Swoole\WebSocket\Serve ... 
- 无刷新上传图片,ajax 和 iframe
			iframe 上传 upload.html 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 ... 
- JavaScript(6)--- 原型链
			原型链 再上一篇有简单讲过原型:JavaScript(5)--- 面向对象 + 原型 讲原型链知识之前,先说几个重要的结论. 1.原型链就是 对象的__proto__所连接的链状结构 2.protot ... 
- Python进阶练习与爬取豆瓣T250的影片相关信息
			(一)Python进阶练习 正所谓要将知识进行实践,才会真正的掌握 于是就练习了几道题:求素数,求奇数,求九九乘法表,字符串练习 import re #求素数 i=1; flag=0 while(i& ... 
- Map - 与君初相识
			前言 生活中,我们常会看到这样一种集合:IP地址与主机名,身份证号与个人等,这种一 一对应的关系,就叫做映射.Java提供了专门的集合类用来存放这种对应关系的对象,即 java.util.Map 接口 ... 
- nohub 将程序永久运行下去
			今天看了一遍文章,一直以为将程序制成sh脚本,通过crontab来间隔执行以为是真的不断执行,后来才发现是错误的,每隔一段时间都会执行一次,都会占用一个进程,难怪一看进程几十来个同样名字的进程在运行 ... 
- Effective Go中文版(更新中)
			原文链接:https://golang.org/doc/effective_go.html Introduction Go是一种新兴的编程语言.虽然它借鉴了现有语言的思想,但它具有不同寻常的特性,使得 ... 
