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中的split()函数的使用方法
Apr 07 Python
django创建自定义模板处理器的实例详解
Aug 14 Python
Python3中正则模块re.compile、re.match及re.search函数用法详解
Jun 11 Python
Python中Proxypool库的安装与配置
Oct 19 Python
Python实现繁体中文与简体中文相互转换的方法示例
Dec 18 Python
django 快速启动数据库客户端程序的方法示例
Aug 16 Python
Tensorflow的常用矩阵生成方式
Jan 04 Python
基于Python计算圆周率pi代码实例
Mar 25 Python
解决django无法访问本地static文件(js,css,img)网页里js,cs都加载不了
Apr 07 Python
浅谈keras使用中val_acc和acc值不同步的思考
Jun 18 Python
Python通过len函数返回对象长度
Oct 22 Python
Python insert() / append() 用法 Leetcode实战演示
Mar 31 Python
Python Pytorch查询图像的特征从集合或数据库中查找图像
Python实现科学占卜 让视频自动打码
Python自动化工具之实现Excel转Markdown表格
Python加密技术之RSA加密解密的实现
Apr 08 #Python
Python识别花卉种类鉴定网络热门植物并自动整理分类
请求模块urllib之PYTHON爬虫的基本使用
用Python仅20行代码编写一个简单的端口扫描器
You might like
PHP5.0对象模型探索之抽象方法和抽象类
2006/09/05 PHP
PHP读取txt文本文件并分页显示的方法
2015/03/11 PHP
php使用curl实现ftp文件下载功能
2017/05/16 PHP
PHP实现基于面向对象的mysqli扩展库增删改查操作工具类
2017/07/18 PHP
JS按位非(~)运算符与~~运算符的理解分析
2011/07/31 Javascript
JavaScript版的TwoQueues缓存模型
2014/12/29 Javascript
JavaScript中的splice()方法使用详解
2015/06/09 Javascript
jQuery hover事件简单实现同时绑定2个方法
2016/06/07 Javascript
js改变html的原有内容实现方法
2016/10/05 Javascript
JavaScript仿支付宝6位数字密码输入框
2016/12/29 Javascript
AngularJS页面带参跳转及参数解析操作示例
2017/06/28 Javascript
vue组件 keep-alive 和 transition 使用详解
2019/10/11 Javascript
Layui数据表格之单元格编辑方式
2019/10/26 Javascript
vue组件传值的实现方式小结【三种方式】
2020/02/05 Javascript
koa2的中间件功能及应用示例
2020/03/05 Javascript
js实现随机点名器精简版
2020/06/29 Javascript
[01:35]2014DOTA2西雅图邀请赛 专访狐狸妈青春献给刀塔
2014/07/08 DOTA
Python爬取APP下载链接的实现方法
2016/09/30 Python
python PyTorch预训练示例
2018/02/11 Python
Python处理中文标点符号大集合
2018/05/14 Python
python递归实现快速排序
2018/08/18 Python
Python实现将Excel转换成为image的方法
2018/10/23 Python
解决使用PyCharm时无法启动控制台的问题
2019/01/19 Python
瑞士灯具购物网站:Lampenwelt.ch
2018/07/08 全球购物
英国蜡烛、蜡烛配件和家居香氛购买网站:Yankee Candle
2018/12/12 全球购物
最新会计专业求职信范文
2014/01/28 职场文书
有兼职工作经历的简历自我评价
2014/03/07 职场文书
2014年大学生党课心得体会范文
2014/03/29 职场文书
小学生综合素质评语
2014/04/23 职场文书
调查研究项目计划书
2014/04/29 职场文书
医院办公室主任岗位职责
2015/04/01 职场文书
催款函范本大全
2015/06/24 职场文书
单位领导婚礼致辞
2015/07/28 职场文书
CSS 还能这样玩?奇思妙想渐变的艺术
2021/04/27 HTML / CSS
分析MySQL抛出异常的几种常见解决方式
2021/05/18 MySQL
python 进阶学习之python装饰器小结
2021/09/04 Python