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 lambda和Python def区别分析
Nov 30 Python
对numpy 数组和矩阵的乘法的进一步理解
Apr 04 Python
python爬虫之urllib,伪装,超时设置,异常处理的方法
Dec 19 Python
Flask核心机制之上下文源码剖析
Dec 25 Python
简单了解python的内存管理机制
Jul 08 Python
python实现两个字典合并,两个list合并
Dec 02 Python
python  ceiling divide 除法向上取整(或小数向上取整)的实例
Dec 27 Python
keras模型可视化,层可视化及kernel可视化实例
Jan 24 Python
python使用ctypes调用扩展模块的实例方法
Jan 28 Python
django执行原始查询sql,并返回Dict字典例子
Apr 01 Python
python实现每天自动签到领积分的示例代码
Aug 18 Python
Python与C/C++的相互调用案例
Mar 04 Python
Python Pytorch查询图像的特征从集合或数据库中查找图像
Python实现科学占卜 让视频自动打码
Python自动化工具之实现Excel转Markdown表格
Python加密技术之RSA加密解密的实现
Apr 08 #Python
Python识别花卉种类鉴定网络热门植物并自动整理分类
请求模块urllib之PYTHON爬虫的基本使用
用Python仅20行代码编写一个简单的端口扫描器
You might like
php 购物车实例(申精)
2009/05/11 PHP
PHP实现HTML生成PDF文件的方法
2014/11/07 PHP
在phpstudy集成环境下的nginx服务器下配置url重写
2019/12/02 PHP
KindEditor在php环境下上传图片功能集成的方法示例
2020/07/20 PHP
javascritp实现input输入框相关限制用法
2007/06/29 Javascript
一个级联菜单代码学习及removeClass与addClass的应用
2013/01/24 Javascript
window.location.href的用法(动态输出跳转)
2014/08/09 Javascript
仿百度联盟对联广告实现代码
2014/08/30 Javascript
JavaScript字符串对象的concat方法实例(用于连接两个或多个字符串)
2014/10/16 Javascript
JS实现仿FLASH效果的竖排导航代码
2015/09/15 Javascript
深入理解jQuery layui分页控件的使用
2016/08/17 Javascript
Javascript计算二维数组重复值示例代码
2016/12/18 Javascript
利用Angularjs中模块ui-route管理状态的方法
2016/12/27 Javascript
Bootstrap中glyphicons-halflings-regular.woff字体报404错notfound的解决方法
2017/01/19 Javascript
vue分类筛选filter方法简单实例
2017/03/30 Javascript
React-Native使用Mobx实现购物车功能
2017/09/14 Javascript
微信小程序公用参数与公用方法用法示例
2019/01/09 Javascript
利用es6 new.target来对模拟抽象类的方法
2019/05/10 Javascript
解决vue项目运行npm run serve报错的问题
2020/10/26 Javascript
[49:35]2018DOTA2亚洲邀请赛3月30日 小组赛A组 KG VS TNC
2018/03/31 DOTA
python中关于日期时间处理的问答集锦
2013/03/08 Python
Python数据结构之翻转链表
2017/02/25 Python
详解Python map函数及Python map()函数的用法
2017/11/16 Python
用python实现将数组元素按从小到大的顺序排列方法
2018/07/02 Python
HTML5 Blob 实现文件下载功能的示例代码
2019/11/29 HTML / CSS
英国健身仓库:Bodybuilding Warehouse
2019/03/06 全球购物
奥地利领先的在线药房:SHOP APOTHEKE
2019/10/07 全球购物
全球最受追捧的运动服品牌领先数字目的地:Stylerunner
2020/11/25 全球购物
进程的查看和调度分别使用什么命令
2015/03/25 面试题
EJB timer的种类
2014/10/28 面试题
学院书画协会部门岗位职责
2013/12/01 职场文书
建筑总经理岗位职责
2014/02/02 职场文书
大学生怎样写好自荐信
2014/02/25 职场文书
贷款委托书范本
2014/04/08 职场文书
教师批评与自我批评(群众路线)
2014/10/15 职场文书
党员廉洁自律个人总结
2015/02/13 职场文书