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中的Descriptor描述符学习教程
Jun 02 Python
利用Python中unittest实现简单的单元测试实例详解
Jan 09 Python
小白如何入门Python? 制作一个网站为例
Mar 06 Python
Python unittest单元测试框架总结
Sep 08 Python
详解Python中正则匹配TAB及空格的小技巧
Jul 26 Python
Python如何优雅获取本机IP方法
Nov 10 Python
Django使用消息提示简单的弹出个对话框实例
Nov 15 Python
Python socket处理client连接过程解析
Mar 18 Python
Selenium alert 弹窗处理的示例代码
Aug 06 Python
pycharm专业版远程登录服务器的详细教程
Sep 15 Python
利用 Python 的 Pandas和 NumPy 库来清理数据
Apr 13 Python
python实现简单的三子棋游戏
Apr 28 Python
Python Pytorch查询图像的特征从集合或数据库中查找图像
Python实现科学占卜 让视频自动打码
Python自动化工具之实现Excel转Markdown表格
Python加密技术之RSA加密解密的实现
Apr 08 #Python
Python识别花卉种类鉴定网络热门植物并自动整理分类
请求模块urllib之PYTHON爬虫的基本使用
用Python仅20行代码编写一个简单的端口扫描器
You might like
PDO版本问题 Invalid parameter number: no parameters were bound
2013/01/06 PHP
Laravel5.1数据库连接、创建数据库、创建model及创建控制器的方法
2016/03/29 PHP
基于PHP制作验证码
2016/10/12 PHP
PHP使用PDO操作sqlite数据库应用案例
2019/03/07 PHP
javascript 写类方式之四
2009/07/05 Javascript
兼容ie、firefox的图片自动缩放的css跟js代码分享
2012/01/21 Javascript
JS的replace方法介绍
2012/10/20 Javascript
window.opener用法和用途实例介绍
2013/08/19 Javascript
js利用数组length属性清空和截短数组的小例子
2014/01/15 Javascript
javascript自定义函数参数传递为字符串格式
2014/07/29 Javascript
基于jquery的手风琴图片展示效果实现方法
2014/12/16 Javascript
深入理解JavaScript系列(45):代码复用模式(避免篇)详解
2015/03/04 Javascript
Jquery attr()方法 属性赋值和属性获取详解
2016/04/15 Javascript
JS实现将数字金额转换为大写人民币汉字的方法
2016/08/02 Javascript
vue checkbox 全选 数据的绑定及获取和计算方法
2018/02/09 Javascript
微信小程序实现运动步数排行功能(可删除)
2018/07/05 Javascript
swiper在angularjs中使用循环轮播失效的解决方法
2018/09/27 Javascript
js类的继承定义与用法分析
2019/06/21 Javascript
JS开发常用工具函数(小结)
2019/07/04 Javascript
layui 表单标签的校验方法
2019/09/04 Javascript
node.js通过Sequelize 连接MySQL的方法
2020/12/28 Javascript
Python中正则表达式详解
2017/05/17 Python
基于Numba提高python运行效率过程解析
2020/03/02 Python
Python趣味实例,实现一个简单的抽奖刮刮卡
2020/07/18 Python
python绘制分布折线图的示例
2020/09/24 Python
python调用有道智云API实现文件批量翻译
2020/10/10 Python
惠普墨西哥官方商店:HP墨西哥
2016/12/01 全球购物
阿迪达斯印度官方商城:adidas India
2017/03/26 全球购物
物业管理个人自我评价
2013/11/08 职场文书
经济管理毕业生求职信
2014/03/15 职场文书
腾讯广告词
2014/03/19 职场文书
农民工工资发放承诺书
2014/03/31 职场文书
股东协议书
2014/04/14 职场文书
2016年秋季运动会通讯稿
2015/11/25 职场文书
初中历史教学反思
2016/02/19 职场文书
关于SpringBoot 使用 Redis 分布式锁解决并发问题
2021/11/17 Redis