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代码的打包与发布详解
Jul 30 Python
Python中使用Flask、MongoDB搭建简易图片服务器
Feb 04 Python
Python学习小技巧之列表项的排序
May 20 Python
Centos下实现安装Python3.6和Python2共存
Aug 15 Python
Python3实现爬虫爬取赶集网列表功能【基于request和BeautifulSoup模块】
Dec 05 Python
PyQt5实现简单数据标注工具
Mar 18 Python
python读取目录下所有的jpg文件,并显示第一张图片的示例
Jun 13 Python
Python使用selenium + headless chrome获取网页内容的方法示例
Oct 16 Python
django实现类似触发器的功能
Nov 15 Python
python 如何去除字符串头尾的多余符号
Nov 19 Python
pytorch 实现模型不同层设置不同的学习率方式
Jan 06 Python
Python编程快速上手——strip()函数的正则表达式实现方法分析
Feb 29 Python
Python Pytorch查询图像的特征从集合或数据库中查找图像
Python实现科学占卜 让视频自动打码
Python自动化工具之实现Excel转Markdown表格
Python加密技术之RSA加密解密的实现
Apr 08 #Python
Python识别花卉种类鉴定网络热门植物并自动整理分类
请求模块urllib之PYTHON爬虫的基本使用
用Python仅20行代码编写一个简单的端口扫描器
You might like
PHP7新增运算符用法实例分析
2016/09/26 PHP
php判断str字符串是否是xml格式数据的方法示例
2017/07/26 PHP
微信公众平台开发教程④ ThinkPHP框架下微信支付功能图文详解
2019/04/10 PHP
静态的动态续篇之来点XML
2006/12/23 Javascript
不使用中间变量,交换int型的 a, b两个变量的值。
2010/10/29 Javascript
javascript禁用Tab键脚本实例
2013/11/22 Javascript
js每隔5分钟执行一次ajax请求的实现方法
2013/11/27 Javascript
页面js遇到乱码问题的解决方法是和无法转码的情况
2014/04/30 Javascript
window.location.href的用法(动态输出跳转)
2014/08/09 Javascript
js实现同一页面多个不同运动效果的方法
2015/04/10 Javascript
JavaScript实现审核流程状态的动态显示进度条
2017/03/15 Javascript
利用Vue v-model实现一个自定义的表单组件
2017/04/27 Javascript
JavaScript 数组的进化与性能分析
2017/09/18 Javascript
JS简单实现点击跳转登陆邮箱功能的方法
2017/10/31 Javascript
Node中使用ES6语法的基础教程
2018/01/05 Javascript
javascript获取图片的top N主色值方法详解
2018/01/26 Javascript
Vue slot用法(小结)
2018/10/22 Javascript
JavaScript闭包与作用域链实例分析
2019/01/21 Javascript
[15:46]教你分分钟做大人——沙王
2015/03/11 DOTA
Python3.5 Pandas模块之DataFrame用法实例分析
2019/04/23 Python
python中break、continue 、exit() 、pass终止循环的区别详解
2019/07/08 Python
Django项目使用CircleCI的方法示例
2019/07/14 Python
Python实现微信好友的数据分析
2019/12/16 Python
Python RabbitMQ实现简单的进程间通信示例
2020/07/02 Python
应届生护士求职信
2013/11/01 职场文书
最新教师自我评价分享
2013/11/12 职场文书
平面设计师工作职责范文
2013/12/03 职场文书
成语的广告词
2014/03/19 职场文书
公司收款委托书范本
2014/09/20 职场文书
竞聘报告优秀范文
2014/11/06 职场文书
警告通知
2015/04/25 职场文书
2015年法务工作总结范文
2015/05/23 职场文书
小学校本教研总结
2015/08/13 职场文书
2016年世界人口日宣传活动总结
2016/04/05 职场文书
创业计划书之寿司
2019/07/19 职场文书
Python下载商品数据并连接数据库且保存数据
2022/03/31 Python