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 冒泡,选择,插入排序使用实例
Feb 05 Python
Python计算三角函数之asin()方法的使用
May 15 Python
详解Python的Django框架中manage命令的使用与扩展
Apr 11 Python
关于Python元祖,列表,字典,集合的比较
Jan 06 Python
详解Django中六个常用的自定义装饰器
Jul 04 Python
详解分布式任务队列Celery使用说明
Nov 29 Python
Python Numpy库datetime类型的处理详解
Jul 13 Python
django ORM之values和annotate使用详解
May 19 Python
python怎么提高计算速度
Jun 11 Python
python利用appium实现手机APP自动化的示例
Jan 26 Python
Matlab使用Plot函数实现数据动态显示方法总结
Feb 25 Python
4种方法python批量修改替换列表中元素
Apr 07 Python
Python Pytorch查询图像的特征从集合或数据库中查找图像
Python实现科学占卜 让视频自动打码
Python自动化工具之实现Excel转Markdown表格
Python加密技术之RSA加密解密的实现
Apr 08 #Python
Python识别花卉种类鉴定网络热门植物并自动整理分类
请求模块urllib之PYTHON爬虫的基本使用
用Python仅20行代码编写一个简单的端口扫描器
You might like
目录,文件操作详谈―PHP
2006/11/25 PHP
php实现快速排序法函数代码
2012/08/27 PHP
smarty 缓存控制前的页面静态化原理
2013/03/15 PHP
PHP static局部静态变量和全局静态变量总结
2014/03/02 PHP
thinkphp3.x自定义Action、Model及View的简单实现方法
2016/05/19 PHP
php解决和避免form表单重复提交的几种方法
2016/08/31 PHP
javascript各种复制代码收集
2008/09/20 Javascript
javascript 解决表单仍然提交即使监听处理函数返回false
2010/03/14 Javascript
jQuery 选择器项目实例分析及实现代码
2012/12/28 Javascript
js 时间函数应用加、减、比较、格式转换的示例代码
2013/08/23 Javascript
javascript根据像素点取位置示例
2014/01/27 Javascript
jquery form 加载数据示例
2014/04/21 Javascript
js实现的万能flv网页播放器代码
2016/04/30 Javascript
所见即所得的富文本编辑器bootstrap-wysiwyg使用方法详解
2016/05/27 Javascript
用NodeJS实现批量查询地理位置的经纬度接口
2016/08/16 NodeJs
详解vue-meta如何让你更优雅的管理头部标签
2018/01/18 Javascript
浅谈js中的attributes和Attribute的用法与区别
2020/07/16 Javascript
[02:08]我的刀塔不可能这么可爱 胡晓桃_1
2014/06/20 DOTA
[52:14]VG vs Serenity 2018国际邀请赛小组赛BO2 第一场 8.17
2018/08/20 DOTA
Python中使用MELIAE分析程序内存占用实例
2015/02/18 Python
python编程实现归并排序
2017/04/14 Python
Python PyQt4实现QQ抽屉效果
2018/04/20 Python
python range()函数取反序遍历sequence的方法
2018/06/25 Python
Django框架 查询Extra功能实现解析
2019/09/04 Python
Python 存取npy格式数据实例
2020/07/01 Python
python切片作为占位符使用实例讲解
2021/02/17 Python
毕业生求职简历的自我评价
2013/10/23 职场文书
大学生毕业自我鉴定范文
2013/11/03 职场文书
业务员岗位职责
2013/11/16 职场文书
《雾凇》教学反思
2014/02/17 职场文书
小学毕业感言300字
2014/02/19 职场文书
感恩小明星事迹材料
2014/05/23 职场文书
安全保证书
2015/01/16 职场文书
团员自我评价范文
2015/03/10 职场文书
Mysql MVCC机制原理详解
2021/04/20 MySQL
小程序实现筛子抽奖
2021/05/26 Javascript