模块 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 ...
随机推荐
- windows下tensorflow/objectdetection API环境搭建(基于tensorflow1.14和python3.6)
此前就听闻室友说tensorflow在windows下坑很多,这次终于亲身领会到了.以下是参考网上大佬的教程以及自己的踩坑史总结出的有效步骤(亲测有效) 1.下载objectdetection所在的m ...
- criteria.setCacheable(true);这个方法是干什么用的
criteria.setCacheable(true); 一下是Criteria的底层源代码 /** * Enable caching of this query result, provided q ...
- DvaJS入门课
不管是Vue还是React,他们都没解决组件间的通信和数据流问题.当然,这个说法不是很准确,准确的说法是他们都没很好的处理这些问题.我们是可以用一些烂手段去解决这个问题,但是当应用比较大.数据多的时候 ...
- Win10系统下安装tensorflow(cpu)+keras+jupyter notebook运行环境
记录,自用 1.安装Anaconda(这里安装的是python3.6版本) 2.创建tensorflow的conda环境 conda create -n tensorflow python=3.6 3 ...
- 必备技能六、Vue中实现全局方法
现实背景:很多时候我们会在全局调用一些方法. 实现方式两种:官网的实现use方法,然后你也可以用野路子直接在Vue.prototype上面定义. 先说野路子,因为其实野路子就是最根本的实现方式,官方的 ...
- 日常破解---XCTF_APP1获取flag记录
日常破解---XCTF_APP1获取flag记录 一.题目来源 来源:XCTF社区安卓题目app1 二.解题记录 1.首先安装到模拟器中运行一下,如下图所示,点击一下按钮,弹出提示年轻人不 ...
- python使用while循环实现九九乘法表
a = 1while a <= 9: b = 1 while b <= a: print("%d*%d=%d\t" % (b, a, a * b), end=" ...
- Go组件学习:如何读取ini配置文件
代码示例全部保存在,欢迎star:https://github.com/EnochZg/golang-examples 安装组件 go get gopkg.in/ini.v1 使用 先创建ini后缀的 ...
- LeetCode 136、137、260(只出现一次的数,异或性质及应用)
First. 陈列一下“异或”的一些性质 异或是一种基于二进制的位运算,用符号XOR或者 ^ 表示,其运算法则是对运算符两侧数的每一个二进制位,同值取0,异值取1. 它与布尔运算的区别在于,当运算符两 ...
- drf(请求封装/认证/权限/节流)
1.请求的封装 class HttpRequest(object): def __init__(self): pass @propery def GET(self): pass @propery de ...