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模块学习 datetime介绍
Aug 27 Python
Python实现从百度API获取天气的方法
Mar 11 Python
基于python的字节编译详解
Sep 20 Python
Python基础语言学习笔记总结(精华)
Nov 14 Python
Python绘制3d螺旋曲线图实例代码
Dec 20 Python
Python学生成绩管理系统简洁版
Apr 05 Python
Python对象属性自动更新操作示例
Jun 15 Python
Python 访问限制 private public的详细介绍
Oct 16 Python
PyPDF2读取PDF文件内容保存到本地TXT实例
May 12 Python
解决Keras中CNN输入维度报错问题
Jun 29 Python
Python requests上传文件实现步骤
Sep 15 Python
python解压zip包中文乱码解决方法
Nov 27 Python
Python Pytorch查询图像的特征从集合或数据库中查找图像
Python实现科学占卜 让视频自动打码
Python自动化工具之实现Excel转Markdown表格
Python加密技术之RSA加密解密的实现
Apr 08 #Python
Python识别花卉种类鉴定网络热门植物并自动整理分类
请求模块urllib之PYTHON爬虫的基本使用
用Python仅20行代码编写一个简单的端口扫描器
You might like
php 多关键字 高亮显示实现代码
2012/04/23 PHP
php数据结构与算法(PHP描述) 查找与二分法查找
2012/06/21 PHP
php curl上传、下载、https登陆实现代码
2017/07/23 PHP
PHP实现单例模式建立数据库连接的方法分析
2020/02/11 PHP
JavaScript 高级篇之DOM文档,简单封装及调用、动态添加、删除样式(六)
2012/04/07 Javascript
如何使用jquery动态加载js,css文件实现代码
2013/04/03 Javascript
java、javascript实现附件下载示例
2014/08/14 Javascript
node.js中的querystring.escape方法使用说明
2014/12/10 Javascript
基于nodejs+express(4.x+)实现文件上传功能
2015/11/23 NodeJs
javascript代码调试之console.log 用法图文详解
2016/09/30 Javascript
JavaScript创建对象的七种方式(推荐)
2017/06/26 Javascript
jQuery扩展方法实现Form表单与Json互相转换的实例代码
2018/09/05 jQuery
在webstorm开发微信小程序之使用阿里自定义字体图标的方法
2018/11/15 Javascript
react脚手架如何配置less和ant按需加载的方法步骤
2018/11/28 Javascript
vue常用高阶函数及综合实例
2021/02/25 Vue.js
[02:55]含熏伴清风,风行者至宝、屠夫身心及典藏宝瓶二展示
2020/09/08 DOTA
详解MySQL数据类型int(M)中M的含义
2016/11/20 Python
Python中functools模块函数解析
2017/03/12 Python
python中plot实现即时数据动态显示方法
2018/06/22 Python
python opencv实现切变换 不裁减图片
2018/07/26 Python
CentOS6.9 Python环境配置(python2.7、pip、virtualenv)
2019/05/06 Python
python opencv minAreaRect 生成最小外接矩形的方法
2019/07/01 Python
Python装饰器原理与基本用法分析
2020/01/07 Python
python重要函数eval多种用法解析
2020/01/14 Python
python中数字是否为可变类型
2020/07/08 Python
Django后端按照日期查询的方法教程
2021/02/28 Python
MSC邮轮官方网站:加勒比海、地中海和世界各地的假期
2018/08/27 全球购物
网络工程专业毕业生推荐信
2013/10/28 职场文书
12.4法制宣传日活动总结
2014/08/26 职场文书
化工见习报告范文
2014/10/31 职场文书
2014年学生工作总结
2014/11/20 职场文书
《狮子和鹿》教学反思
2016/02/16 职场文书
假期读书倡议书3篇
2019/08/19 职场文书
2020年元旦晚会策划书模板
2019/12/30 职场文书
剑指Offer之Java算法习题精讲二叉树专项训练
2022/03/21 Java/Android
Redis主从复制操作和配置详情
2022/09/23 Redis