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网络编程学习笔记(九):数据库客户端 DB-API
Jun 09 Python
跟老齐学Python之有点简约的元组
Sep 24 Python
Python实现的递归神经网络简单示例
Aug 11 Python
python的numpy模块安装不成功简单解决方法总结
Dec 23 Python
pandas series序列转化为星期几的实例
Apr 11 Python
python tornado微信开发入门代码
Aug 24 Python
python2与python3的print及字符串格式化小结
Nov 30 Python
在Pycharm中调试Django项目程序的操作方法
Jul 17 Python
python logging 重复写日志问题解决办法详解
Aug 04 Python
关于PyCharm安装后修改路径名称使其可重新打开的问题
Oct 20 Python
python使用dlib进行人脸检测和关键点的示例
Dec 05 Python
python数据抓取3种方法总结
Feb 07 Python
Python Pytorch查询图像的特征从集合或数据库中查找图像
Python实现科学占卜 让视频自动打码
Python自动化工具之实现Excel转Markdown表格
Python加密技术之RSA加密解密的实现
Apr 08 #Python
Python识别花卉种类鉴定网络热门植物并自动整理分类
请求模块urllib之PYTHON爬虫的基本使用
用Python仅20行代码编写一个简单的端口扫描器
You might like
php目录遍历函数opendir用法实例
2014/11/20 PHP
PHP实现自动识别Restful API的返回内容类型
2015/02/07 PHP
ECSHOP完美解决Deprecated: preg_replace()报错的问题
2016/05/17 PHP
Yii框架用户登录session丢失问题解决方法
2017/01/07 PHP
PHP中empty,isset,is_null用法和区别
2017/02/19 PHP
thinkphp3.2实现跨控制器调用其他模块的方法
2017/03/14 PHP
Yii框架批量插入数据扩展类的简单实现方法
2017/05/23 PHP
PHP实现双链表删除与插入节点的方法示例
2017/11/11 PHP
php的无刷新操作实现方法分析
2020/02/28 PHP
PHP重载基础知识回顾
2020/09/10 PHP
JS 获取浏览器和屏幕宽高等信息的实现思路及代码
2013/07/31 Javascript
javascript日期计算实例分析
2015/06/29 Javascript
jquery对象和DOM对象的任意相互转换
2016/02/21 Javascript
Node.js程序中的本地文件操作用法小结
2016/03/06 Javascript
JQuery异步加载PartialView的方法
2016/06/07 Javascript
Google 地图事件实例讲解
2016/08/06 Javascript
vue插件tab选项卡使用小结
2016/10/27 Javascript
bootstrap Table服务端处理分页(后台是.net)
2017/10/19 Javascript
Vue中使用vee-validate表单验证的方法
2018/05/09 Javascript
vue项目打包部署_nginx代理访问方法详解
2018/09/20 Javascript
详解微信小程序网络请求接口封装实例
2019/05/02 Javascript
Python复制目录结构脚本代码分享
2015/03/06 Python
Python编程对列表中字典元素进行排序的方法详解
2017/05/26 Python
在windows下Python打印彩色字体的方法
2018/05/15 Python
解决在pycharm中显示额外的 figure 窗口问题
2019/01/15 Python
Python3调用百度AI识别图片中的文字功能示例【测试可用】
2019/03/13 Python
django celery redis使用具体实践
2019/04/08 Python
对python3.4 字符串转16进制的实例详解
2019/06/12 Python
pytorch::Dataloader中的迭代器和生成器应用详解
2020/01/03 Python
Ubuntu16.04安装python3.6.5步骤详解
2020/01/10 Python
Python操作MySQL数据库实例详解【安装、连接、增删改查等】
2020/01/17 Python
深入研究HTML5实现图片压缩上传功能
2016/03/25 HTML / CSS
HTML5实现直播间评论滚动效果的代码
2020/05/27 HTML / CSS
电台编导求职信
2014/05/06 职场文书
java项目构建Gradle的使用教程
2022/03/24 Java/Android
Moment的feature导致线上bug解决分析
2022/09/23 Javascript