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群发邮件实例代码
Jan 03 Python
python实现的正则表达式功能入门教程【经典】
Jun 05 Python
Python简单计算文件MD5值的方法示例
Apr 11 Python
使用python中的in ,not in来检查元素是不是在列表中的方法
Jul 06 Python
python中类的属性和方法介绍
Nov 27 Python
解决pyttsx3无法封装的问题
Dec 24 Python
python DataFrame 取差集实例
Jan 30 Python
基于python3 pyQt5 QtDesignner实现窗口化猜数字游戏功能
Jul 15 Python
Django REST Framework之频率限制的使用
Sep 29 Python
pytorch加载自定义网络权重的实现
Jan 07 Python
Python GUI编程学习笔记之tkinter控件的介绍及基本使用方法详解
Mar 30 Python
Keras在训练期间可视化训练误差和测试误差实例
Jun 16 Python
Python Pytorch查询图像的特征从集合或数据库中查找图像
Python实现科学占卜 让视频自动打码
Python自动化工具之实现Excel转Markdown表格
Python加密技术之RSA加密解密的实现
Apr 08 #Python
Python识别花卉种类鉴定网络热门植物并自动整理分类
请求模块urllib之PYTHON爬虫的基本使用
用Python仅20行代码编写一个简单的端口扫描器
You might like
浅谈PDO的rowCount函数
2015/06/18 PHP
PHP读取文件的常见几种方法
2016/11/03 PHP
Laravel 实现Eloquent模型分组查询并返回每个分组的数量 groupBy()
2019/10/23 PHP
鼠标图片振动代码
2006/07/06 Javascript
Javascript中的常见排序算法
2007/03/27 Javascript
用js生产批量批处理执行命令
2008/07/28 Javascript
很多人都是用下面的js刷新站IP和PV
2008/09/05 Javascript
解javascript 混淆加密收藏
2009/01/16 Javascript
百度 popup.js 完美修正版非常的不错 脚本之家推荐
2009/04/17 Javascript
js控制浏览器全屏示例代码
2014/02/20 Javascript
javascript生成大小写字母
2015/07/03 Javascript
整理Javascript事件响应学习笔记
2015/12/02 Javascript
BootStrap使用popover插件实现鼠标经过显示并保持显示框
2016/06/23 Javascript
jQuery多文件异步上传带进度条实例代码
2016/08/16 Javascript
JavaScript鼠标特效大全
2016/09/13 Javascript
js仿手机页面文件下拉刷新效果
2016/10/14 Javascript
JavaScript 数组的深度复制解析
2016/11/02 Javascript
js return返回多个值,通过对象的属性访问方法
2017/02/21 Javascript
基于Vue实例生命周期(全面解析)
2017/08/16 Javascript
基于Vue2.X的路由和钩子函数详解
2018/02/09 Javascript
layui富文本编辑器前端无法取值的解决方法
2019/09/18 Javascript
解决vuex刷新数据消失问题
2020/11/12 Javascript
javascript全局自定义鼠标右键菜单
2020/12/08 Javascript
python中for循环输出列表索引与对应的值方法
2018/11/07 Python
python 画函数曲线示例
2019/12/04 Python
Keras使用tensorboard显示训练过程的实例
2020/02/15 Python
Python实现一个优先级队列的方法
2020/07/31 Python
CSS3解析抖音LOGO制作的方法步骤
2019/04/11 HTML / CSS
微信浏览器左上角返回按钮拦截功能
2017/11/21 HTML / CSS
阿根廷网上配眼镜:SmartBuyGlasses阿根廷
2016/08/19 全球购物
世界上最好的威士忌和烈性酒购买网站:The Whisky Exchange
2016/11/20 全球购物
艺术应用与设计专业个人的自我评价
2013/11/19 职场文书
乡镇干部党的群众路线教育实践活动个人对照检查材料
2014/09/24 职场文书
2016年小学生寒假总结
2015/10/10 职场文书
阿里云服务器搭建Php+Apache运行环境的详细过程
2021/05/15 PHP
Anaconda安装pytorch及配置PyCharm 2021环境
2021/06/04 Python