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 相关文章推荐
python3使用tkinter实现ui界面简单实例
Jan 10 Python
python编码最佳实践之总结
Feb 14 Python
numpy基础教程之np.linalg
Feb 12 Python
pandas数据集的端到端处理
Feb 18 Python
Python中按键来获取指定的值
Mar 02 Python
Python发展史及网络爬虫
Jun 19 Python
Python Django Vue 项目创建过程详解
Jul 29 Python
python 读取串口数据的示例
Nov 09 Python
如何在 Matplotlib 中更改绘图背景的实现
Nov 26 Python
python批量合成bilibili的m4s缓存文件为MP4格式 ver2.5
Dec 01 Python
使用Pytorch训练two-head网络的操作
May 28 Python
关于 Python json中load和loads区别
Nov 07 Python
Python Pytorch查询图像的特征从集合或数据库中查找图像
Python实现科学占卜 让视频自动打码
Python自动化工具之实现Excel转Markdown表格
Python加密技术之RSA加密解密的实现
Apr 08 #Python
Python识别花卉种类鉴定网络热门植物并自动整理分类
请求模块urllib之PYTHON爬虫的基本使用
用Python仅20行代码编写一个简单的端口扫描器
You might like
轻松入门: 煮好咖啡的七个诀窍
2021/03/03 冲泡冲煮
计算php页面运行时间的函数介绍
2013/07/01 PHP
thinkphp的静态缓存用法分析
2014/11/29 PHP
php实现的简单日志写入函数
2015/03/31 PHP
WordPress中邮件的一些修改和自定义技巧
2015/12/15 PHP
yii2 modal弹窗之ActiveForm ajax表单异步验证
2016/06/13 PHP
Thinkphp 框架配置操作之动态配置、扩展配置及批量配置实例分析
2020/05/15 PHP
可输入的下拉框
2006/06/19 Javascript
JavaScript 在各个浏览器中执行的耐性
2009/04/06 Javascript
javascript 关于# 和 void的区别分析
2009/10/26 Javascript
js 函数调用模式小结
2011/12/26 Javascript
jQuery实现dialog设置focus焦点的方法
2015/06/10 Javascript
JS实现点击按钮获取页面高度的方法
2015/11/02 Javascript
Bootstrap树形控件使用方法详解
2016/01/27 Javascript
原生js实现下拉框功能(支持键盘事件)
2017/01/13 Javascript
react+ant design实现Table的增、删、改的示例代码
2018/12/27 Javascript
微信小程序的引导页实现代码
2020/06/24 Javascript
简单易懂的python环境安装教程
2017/07/13 Python
Python 实现一行输入多个值的方法
2018/04/21 Python
对pandas通过索引提取dataframe的行方法详解
2019/02/01 Python
Django异步任务线程池实现原理
2019/12/17 Python
Python模块的定义,模块的导入,__name__用法实例分析
2020/01/07 Python
pytorch+lstm实现的pos示例
2020/01/14 Python
用pytorch的nn.Module构造简单全链接层实例
2020/01/14 Python
Python如何获取文件指定行的内容
2020/05/27 Python
Pat McGrath Labs官网:世界上最有影响力的化妆师推出的彩妆品牌
2018/01/07 全球购物
获取邓白氏信用报告:Dun & Bradstreet
2019/01/22 全球购物
员工薪酬福利制度
2014/01/17 职场文书
活动志愿者自荐信
2014/01/27 职场文书
制冷与空调专业毕业生推荐信
2014/07/07 职场文书
党的群众路线教育实践活动对照检查材料(教师)
2014/09/24 职场文书
十佳少年事迹材料
2014/12/25 职场文书
2015小学教育教学工作总结
2015/07/21 职场文书
大学学习委员竞选稿
2015/11/20 职场文书
高三化学教学反思
2016/02/22 职场文书
JavaScript中document.activeELement焦点元素介绍
2021/11/27 Javascript