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复制与引用用法分析
Apr 08 Python
Python中逗号的三种作用实例分析
Jun 08 Python
在Python的Django框架中包装视图函数
Jul 20 Python
python字符串连接方法分析
Apr 12 Python
简单谈谈Python中的反转字符串问题
Oct 24 Python
快速了解python leveldb
Jan 18 Python
基于python 爬虫爬到含空格的url的处理方法
May 11 Python
Pycharm 设置默认头的图文教程
Jan 17 Python
正确理解Python中if __name__ == '__main__'
Jan 24 Python
Django网络框架之创建虚拟开发环境操作示例
Jun 06 Python
华为2019校招笔试题之处理字符串(python版)
Jun 25 Python
pyhton中__pycache__文件夹的产生与作用详解
Nov 24 Python
Python Pytorch查询图像的特征从集合或数据库中查找图像
Python实现科学占卜 让视频自动打码
Python自动化工具之实现Excel转Markdown表格
Python加密技术之RSA加密解密的实现
Apr 08 #Python
Python识别花卉种类鉴定网络热门植物并自动整理分类
请求模块urllib之PYTHON爬虫的基本使用
用Python仅20行代码编写一个简单的端口扫描器
You might like
CakePHP去除默认显示的标题及图标的方法
2008/10/22 PHP
ecshop 批量上传(加入自定义属性)
2012/03/20 PHP
PHP实现的比较完善的购物车类
2014/12/02 PHP
php操作xml入门之xml基本介绍及xml标签元素
2015/01/23 PHP
PHP中call_user_func_array回调函数的用法示例
2016/11/26 PHP
LNMP部署laravel以及xhprof安装使用教程
2017/09/14 PHP
总结PHP代码规范、流程规范、git规范
2018/06/18 PHP
详解php中curl返回false的解决办法
2019/03/18 PHP
详解php命令注入攻击
2019/04/06 PHP
两个SUBMIT按钮,如何区分处理
2006/08/22 Javascript
jQuery textarea的长度进行验证
2009/05/06 Javascript
nodejs中实现阻塞实例
2015/03/24 NodeJs
浅谈javascript事件取消和阻止冒泡
2015/05/26 Javascript
js实现文本框选中的方法
2015/05/26 Javascript
javascript实现不同颜色Tab标签切换效果
2016/04/27 Javascript
bootstrap modal弹出框的垂直居中
2016/12/14 Javascript
JS简单判断函数是否存在的方法
2017/02/13 Javascript
vue2.X组件学习心得(新手必看篇)
2017/07/05 Javascript
angular.js中解决跨域问题的三种方式
2017/07/12 Javascript
详解AngularJS1.x学习directive 中‘& ’‘=’ ‘@’符号的区别使用
2017/08/23 Javascript
js的新生代垃圾回收知识点总结
2019/08/22 Javascript
js+canvas实现简单扫雷小游戏
2021/01/22 Javascript
Element InfiniteScroll无限滚动的具体使用方法
2020/07/27 Javascript
PyQt5每天必学之滑块控件QSlider
2018/04/20 Python
Django 实现前端图片压缩功能的方法
2019/08/07 Python
keras tensorflow 实现在python下多进程运行
2020/02/06 Python
python logging.info在终端没输出的解决
2020/05/12 Python
Pycharm编辑器功能之代码折叠效果的实现代码
2020/10/15 Python
css3使网页、图片变成灰色兼容大多数浏览器
2014/07/02 HTML / CSS
HTML5新增加标签和功能概述
2016/09/05 HTML / CSS
html5视频常用API接口的实战示例
2020/03/20 HTML / CSS
Amara德国:家居饰品、设计师品牌和豪华礼品
2019/05/20 全球购物
澳大利亚购买太阳镜和眼镜网站:Glamoureyes
2020/09/22 全球购物
《浅水洼里的小鱼》听课反思
2014/02/28 职场文书
英语投诉信范文
2015/07/03 职场文书
mysql连接查询中and与where的区别浅析
2021/07/01 MySQL