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初学者的一些编程技巧
Apr 03 Python
浅析python中SQLAlchemy排序的一个坑
Feb 24 Python
Python实现爬虫从网络上下载文档的实例代码
Jun 13 Python
Python http接口自动化测试框架实现方法示例
Dec 06 Python
Python3.5文件修改操作实例分析
May 01 Python
Python 爬虫实现增加播客访问量的方法实现
Oct 31 Python
Python高级编程之消息队列(Queue)与进程池(Pool)实例详解
Nov 01 Python
关于python中plt.hist参数的使用详解
Nov 28 Python
Python一行代码解决矩阵旋转的问题
Nov 30 Python
numpy按列连接两个维数不同的数组方式
Dec 06 Python
浅谈python累加求和+奇偶数求和_break_continue
Feb 25 Python
Python 阶乘详解
Oct 05 Python
Python Pytorch查询图像的特征从集合或数据库中查找图像
Python实现科学占卜 让视频自动打码
Python自动化工具之实现Excel转Markdown表格
Python加密技术之RSA加密解密的实现
Apr 08 #Python
Python识别花卉种类鉴定网络热门植物并自动整理分类
请求模块urllib之PYTHON爬虫的基本使用
用Python仅20行代码编写一个简单的端口扫描器
You might like
用PHP实现小型站点广告管理
2006/10/09 PHP
PHP Squid中可缓存的动态网页设计
2008/09/17 PHP
php实现Session存储到Redis
2015/11/11 PHP
PHP对象克隆clone用法示例
2016/09/28 PHP
PHP中检索字符串的方法分析【strstr与substr_count方法】
2017/02/17 PHP
详细对比php中类继承和接口继承
2018/10/11 PHP
关于jQuery中.attr()和.prop()的问题探讨
2013/09/06 Javascript
jsonp原理及使用
2013/10/28 Javascript
fckeditor粘贴Word时弹出窗口取消的方法
2014/10/30 Javascript
jQuery中DOM树操作之使用反向插入方法实例分析
2015/01/23 Javascript
浅析javascript函数表达式
2016/02/10 Javascript
深入理解JavaScript中的浮点数
2016/05/18 Javascript
js在ie下打开对话窗口的方法小结
2016/10/24 Javascript
原生js实现拖拽功能基本思路详解
2018/04/18 Javascript
小程序从手动埋点到自动埋点的实现方法
2019/01/24 Javascript
微信小程序select下拉框实现效果
2019/05/15 Javascript
jQuery操作元素的内容和样式完整实例分析
2020/01/10 jQuery
详解vue父子组件状态同步的最佳方式
2020/09/10 Javascript
vue实现简单加法计算器
2020/10/22 Javascript
[40:31]Secret vs Alliacne 2019国际邀请赛小组赛 BO2 第二场 8.15
2019/08/17 DOTA
[49:21]完美世界DOTA2联赛循环赛 Ink Ice vs LBZS BO2第二场 11.05
2020/11/06 DOTA
python使用urllib模块和pyquery实现阿里巴巴排名查询
2014/01/16 Python
Scrapy框架爬取西刺代理网免费高匿代理的实现代码
2019/02/22 Python
Python+Appium实现自动化清理微信僵尸好友的方法
2021/02/04 Python
CSS3 translate导致字体模糊的实例代码
2019/08/30 HTML / CSS
HTML5 transform三维立方体实现360无死角三维旋转效果
2014/08/22 HTML / CSS
Ramy Brook官网:美国现代女装品牌
2019/06/18 全球购物
优秀毕业自我鉴定
2014/02/15 职场文书
计算机应届毕业生自荐信范文
2014/02/23 职场文书
供货协议书范本
2014/04/22 职场文书
促销活动总结模板
2014/07/01 职场文书
爱护公共设施演讲稿
2014/09/13 职场文书
新郎结婚保证书
2015/02/26 职场文书
办公经费申请报告
2015/05/15 职场文书
SpringBoot中HttpSessionListener的简单使用方式
2022/03/17 Java/Android
生命的关键成分来自太空?陨石说是的
2022/04/29 数码科技