python图像处理 PIL Image操作实例


Posted in Python onApril 09, 2022

1. 图片加载、灰度图、 显示和保存

from PIL import Image

img = Image.open('01.jpg')
imgGrey = img.convert('L')

img.show()
imgGrey.show()

img.save('img_copy.jpg')
imgGrey.save('img_gray.jpg')

2. 图片宽、高、通道模式、平均值获取

from PIL import Image
import numpy as np

img = Image.open('01.jpg')

width, height = img.size
channel_mode = img.mode
mean_value = np.mean(img)

print(width)
print(height)
print(channel_mode)
print(mean_value)

3. 创建指定大小,指定通道类型的空图像

from PIL import Image

width = 200
height = 100

img_white = Image.new('RGB', (width,height), (255,255,255))
img_black = Image.new('RGB', (width,height), (0,0,0))
img_L = Image.new('L', (width, height), (255))

img_white.show()
img_black.show()
img_L.show()

4. 访问和操作图像像素

from PIL import Image

img = Image.open('01.jpg')

width, height = img.size

# 获取指定坐标位置像素值
pixel_value = img.getpixel((width/2, height/2))
print(pixel_value)

# 或者使用load方法
pim = img.load()
pixel_value1 = pim[width/2, height/2]
print(pixel_value1)

# 设置指定坐标位置像素的值
pim[width/2, height/2] = (0, 0, 0)

# 或使用putpixel方法
img.putpixel((w//2, h//2), (255,255,255))

# 设置指定区域像素的值
for w in range(int(width/2) - 40, int(width/2) + 40):
for h in range(int(height/2) - 20, int(height/2) + 20):
pim[w, h] = (255, 0, 0)
# img.putpixel((w, h), (255,255,255))
img.show()

5. 图像通道分离和合并

from PIL import Image

img = Image.open('01.jpg')

# 通道分离
R, G, B = img.split()

R.show)
G.show()
B.show()

# 通道合并
img_RGB = Image.merge('RGB', (R, G, B))
img_BGR = Image.merge('RGB', (B, G, R))
img_RGB.show()
img_BGR.show()

6. 在图像上输出文字

from PIL import Image, ImageDraw, ImageFont

img = Image.open('01.jpg')

# 创建Draw对象:
draw = ImageDraw.Draw(img)
# 字体颜色
fillColor = (255, 0, 0)

text = 'print text on PIL Image'
position = (200,100)

draw.text(position, text, fill=fillColor)
img.show()

7. 图像缩放

from PIL import Image

img = Image.open('01.jpg')

width, height = img.size

img_NEARESET = img.resize((width//2, height//2)) # 缩放默认模式是NEARESET(最近邻插值)
img_BILINEAR = img.resize((width//2, height//2), Image.BILINEAR) # BILINEAR 2x2区域的双线性插值
img_BICUBIC = img.resize((width//2, height//2), Image.BICUBIC) # BICUBIC 4x4区域的双三次插值
img_ANTIALIAS = img.resize((width//2, height//2), Image.ANTIALIAS) # ANTIALIAS 高质量下采样滤波

8. 图像遍历操作

from PIL import Image

img = Image.open('01.jpg').convert('L')

width, height = img.size

pim = img.load()

for w in range(width):
for h in range(height):
if pim[w, h] > 100:
img.putpixel((w, h), 255)
# pim[w, h] = 255
else:
img.putpixel((w, h), 0)
# pim[w, h] = 0

img.show()

9. 图像阈值分割、 二值化

from PIL import Image

img = Image.open('01.jpg').convert('L')

width, height = img.size

threshold = 125

for w in range(width):
for h in range(height):
if img.getpixel((w, h)) > threshold:
img.putpixel((w, h), 255)
else:
img.putpixel((w, h), 0)

img.save('binary.jpg')

10. 图像裁剪

from PIL import Image

img = Image.open('01.jpg')

width, height = img.size

# 前两个坐标点是左上角坐标
# 后两个坐标点是右下角坐标
# width在前, height在后
box = (100, 100, 550, 350)

region = img.crop(box)

region.save('crop.jpg')

11. 图像边界扩展

# 边界扩展
from PIL import Image

img = Image.open('test.png')

width, height = img.size
channel_mode = img.mode

img_makeBorder_full = Image.new(channel_mode, (2*width, height))
img_makeBorder_part = Image.new(channel_mode, (width+200, height))

# 图像水平扩展整个图像
img_makeBorder_full.paste(img, (0, 0, width, height))
img_makeBorder_full.paste(img, (width, 0, 2*width, height))

# 前两个坐标点是左上角坐标
# 后两个坐标点是右下角坐标
# width在前, height在后
box = (width-200, 0, width, height)
region = img.crop(box)

# 图像水平右侧扩展一个ROI
img_makeBorder_part.paste(img, (0, 0, width, height))
img_makeBorder_part.paste(region, (width, 0, width+200, height))
img_makeBorder_part.show()
img_makeBorder_full.show()

12. PIL.Image 和 numpy 格式相互转换

from PIL import Image
import numpy as np

img = Image.open('01.jpg')

array = np.array(img) # PIL.Image 转 numpy

img1 = Image.fromarray(array) # numpy转 PIL.Image
img1 = Image.fromarray(array.astype('uint8'))

img1.save('from_array.jpg')
Python 相关文章推荐
Python使用requests发送POST请求实例代码
Jan 25 Python
python中使用psutil查看内存占用的情况
Jun 11 Python
python实现自动网页截图并裁剪图片
Jul 30 Python
numpy向空的二维数组中添加元素的方法
Nov 01 Python
python监测当前联网状态并连接的实例
Dec 18 Python
利用python numpy+matplotlib绘制股票k线图的方法
Jun 26 Python
python使用tomorrow实现多线程的例子
Jul 20 Python
Python函数装饰器原理与用法详解
Aug 16 Python
python字符串格式化方式解析
Oct 19 Python
TensorFlow命名空间和TensorBoard图节点实例
Jan 23 Python
Python基础教程之输入输出和运算符
Jul 26 Python
Python之qq自动发消息的示例代码
Feb 18 Python
Python Pytorch查询图像的特征从集合或数据库中查找图像
Python实现科学占卜 让视频自动打码
Python自动化工具之实现Excel转Markdown表格
Python加密技术之RSA加密解密的实现
Apr 08 #Python
Python识别花卉种类鉴定网络热门植物并自动整理分类
请求模块urllib之PYTHON爬虫的基本使用
用Python仅20行代码编写一个简单的端口扫描器
You might like
php文件操作小结(删除指定文件/获取文件夹下的文件名/读取文件夹下图片名)
2016/05/09 PHP
Laravel 自动生成验证的实例讲解:login / logout
2019/10/14 PHP
Js 本页面传值实现代码
2009/05/17 Javascript
javascript AOP 实现ajax回调函数使用比较方便
2010/11/20 Javascript
jQuery1.3.2 升级到jQuery1.4.4需要修改的地方
2011/01/06 Javascript
jQuery数据缓存功能的实现思路及简单模拟
2013/05/27 Javascript
使用js判断当前时区TimeZone是否是夏令时
2014/02/23 Javascript
浅谈jquery.fn.extend与jquery.extend区别
2015/07/13 Javascript
nodejs入门教程二:创建一个简单应用示例
2017/04/24 NodeJs
JS检测是否可以访问公网服务器功能代码
2017/06/19 Javascript
vue实现tab切换外加样式切换方法
2018/03/16 Javascript
jQuery length 和 size()区别总结
2018/04/26 jQuery
JavaScript callback回调函数用法实例分析
2018/05/08 Javascript
React中使用UEditor百度富文本的方法
2018/08/22 Javascript
layui实现二维码弹窗、并下载到本地的方法
2019/09/25 Javascript
[01:00:14]2018DOTA2亚洲邀请赛 4.6 淘汰赛 VP vs TNC 第三场
2018/04/10 DOTA
[01:01:14]完美世界DOTA2联赛PWL S2 SZ vs Rebirth 第一场 11.21
2020/11/23 DOTA
python读写文件操作示例程序
2013/12/02 Python
分析并输出Python代码依赖的库的实现代码
2015/08/09 Python
python基础教程之while循环
2019/08/14 Python
基于python的列表list和集合set操作
2019/11/24 Python
tensorflow实现测试时读取任意指定的check point的网络参数
2020/01/21 Python
python实现测试工具(二)——简单的ui测试工具
2020/10/19 Python
整理HTML5移动端开发的常用触摸事件
2016/04/15 HTML / CSS
浅谈HTML5 Web Worker的使用
2018/01/05 HTML / CSS
打造经典复古风格的品牌:Alice + Olivia(爱丽丝+奥利维亚)
2016/09/07 全球购物
adidas官方旗舰店:德国运动用品制造商
2017/11/25 全球购物
学前班教师的自我鉴定
2013/12/05 职场文书
我的梦想演讲稿500字
2014/08/21 职场文书
教师学习党的群众路线教育实践活动心得体会
2014/10/31 职场文书
2014年语文教学工作总结
2014/12/17 职场文书
毕业欢送会致辞
2015/07/29 职场文书
史上最全的军训拉歌口号
2015/12/25 职场文书
教你利用python实现企业微信发送消息
2021/05/23 Python
Python图片处理之图片裁剪教程
2021/05/27 Python
解决 Redis 秒杀超卖场景的高并发
2022/04/12 Redis