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 03 Python
Python生成验证码实例
Aug 21 Python
Python简单获取自身外网IP的方法
Sep 18 Python
详解Python 2.6 升级至 Python 2.7 的实践心得
Apr 27 Python
Python环境搭建之OpenCV的步骤方法
Oct 20 Python
python的格式化输出(format,%)实例详解
Jun 01 Python
Selenium鼠标与键盘事件常用操作方法示例
Aug 13 Python
python切片的步进、添加、连接简单操作示例
Jul 11 Python
Python制作词云图代码实例
Sep 09 Python
基于python操作ES实例详解
Nov 16 Python
使用python-opencv读取视频,计算视频总帧数及FPS的实现
Dec 10 Python
python绘制动态曲线教程
Feb 24 Python
Python Pytorch查询图像的特征从集合或数据库中查找图像
Python实现科学占卜 让视频自动打码
Python自动化工具之实现Excel转Markdown表格
Python加密技术之RSA加密解密的实现
Apr 08 #Python
Python识别花卉种类鉴定网络热门植物并自动整理分类
请求模块urllib之PYTHON爬虫的基本使用
用Python仅20行代码编写一个简单的端口扫描器
You might like
PHP 字符截取 解决中文的截取问题,不用mb系列
2009/09/29 PHP
腾讯QQ php程序员面试题目整理
2010/06/08 PHP
php实现图片上传并进行替换操作
2016/03/15 PHP
php+mysql开发的最简单在线题库(在线做题系统)完整案例
2019/03/30 PHP
Laravel向公共模板赋值方法总结
2019/06/25 PHP
YII2框架中actions的作用与使用方法示例
2020/03/13 PHP
JavaScript面向对象设计二 构造函数模式
2011/12/20 Javascript
jQuery学习笔记(4)--Jquery中获取table中某列值的具体思路
2013/04/10 Javascript
offsetHeight在OnLoad中获取为0的现象
2013/07/22 Javascript
使用jQuery和PHP实现类似360功能开关效果
2014/02/12 Javascript
巧用jquery解决下拉菜单被Div遮挡的相关问题
2014/02/13 Javascript
网页运行时提示对象不支持abigimage属性或方法
2014/08/10 Javascript
仿JQuery输写高效JSLite代码的一些技巧
2015/01/13 Javascript
js实现当前输入框高亮显示的方法
2015/08/19 Javascript
angularjs创建弹出框实现拖动效果
2020/08/25 Javascript
jQuery模拟Marquee实现无缝滚动效果完整实例
2016/09/29 Javascript
JS禁止查看网页源代码的实现方法
2016/10/12 Javascript
快速实现jQuery多级菜单效果
2017/02/01 Javascript
jQuery实现的弹幕效果完整实例
2017/09/06 jQuery
微信小程序模拟cookie的实现
2018/06/20 Javascript
vue 使用html2canvas将DOM转化为图片的方法
2018/09/11 Javascript
发布一款npm包帮助理解npm的使用
2019/01/03 Javascript
使用Vue实现调用接口加载页面初始数据
2019/10/28 Javascript
[01:45]DOTA2众星出演!DSPL刀塔次级职业联赛宣传片
2014/11/21 DOTA
使用python在校内发人人网状态(人人网看状态)
2014/02/19 Python
python 循环遍历字典元素的简单方法
2016/09/11 Python
Python如何使用Gitlab API实现批量的合并分支
2019/11/27 Python
浅谈如何使用python抓取网页中的动态数据实现
2020/08/17 Python
Python 操作 MySQL数据库
2020/09/18 Python
css3教程之倾斜页面
2014/01/27 HTML / CSS
自荐信结尾
2013/10/27 职场文书
酒店led欢迎词
2014/01/09 职场文书
高中生期末评语
2014/01/28 职场文书
人力管理专业毕业生求职信
2014/02/27 职场文书
会议承办单位欢迎词
2019/07/09 职场文书
mybatis 解决从列名到属性名的自动映射失败问题
2021/06/30 Java/Android