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语句、函数与方法的使用技巧总结
Dec 08 Python
浅谈Python生成器generator之next和send的运行流程(详解)
May 08 Python
python3使用pyqt5制作一个超简单浏览器的实例
Oct 19 Python
基于python的图片修复程序(实现水印去除)
Jun 04 Python
Python实现的远程文件自动打包并下载功能示例
Jul 12 Python
浅析使用Python搭建http服务器
Oct 27 Python
pygame实现贪吃蛇游戏(下)
Oct 29 Python
tensorflow 重置/清除计算图的实现
Jan 19 Python
Python Tornado批量上传图片并显示功能
Mar 26 Python
MxNet预训练模型到Pytorch模型的转换方式
May 25 Python
Python基于BeautifulSoup爬取京东商品信息
Jun 01 Python
教你用Python+selenium搭建自动化测试环境
Jun 18 Python
Python Pytorch查询图像的特征从集合或数据库中查找图像
Python实现科学占卜 让视频自动打码
Python自动化工具之实现Excel转Markdown表格
Python加密技术之RSA加密解密的实现
Apr 08 #Python
Python识别花卉种类鉴定网络热门植物并自动整理分类
请求模块urllib之PYTHON爬虫的基本使用
用Python仅20行代码编写一个简单的端口扫描器
You might like
全国FM电台频率大全 - 23 四川省
2020/03/11 无线电
php循环检测目录是否存在并创建(循环创建目录)
2011/01/06 PHP
linux下使用crontab实现定时PHP计划任务失败的原因分析
2014/07/05 PHP
JavaScript中的History历史对象
2008/01/16 Javascript
Js参数值中含有单引号或双引号问题的解决方法
2013/11/06 Javascript
window.location的重写及判断location是否被重写
2014/09/04 Javascript
node.js中的fs.rmdirSync方法使用说明
2014/12/16 Javascript
jQuery中ajax的load()方法用法实例
2014/12/26 Javascript
jQuery实现可拖拽的许愿墙效果【附demo源码下载】
2016/09/14 Javascript
微信小程序 教程之模板
2016/10/18 Javascript
如何用js判断dom是否有存在某class的值
2017/02/13 Javascript
Angularjs分页查询的实现
2017/02/24 Javascript
bootstrap模态框远程示例代码分享
2017/05/22 Javascript
node中的cookie的具体使用
2018/09/13 Javascript
分享5个小技巧让你写出更好的 JavaScript 条件语句
2018/10/20 Javascript
JQuery插件tablesorter表格排序实现过程解析
2020/05/28 jQuery
vue-calendar-component 封装多日期选择组件的实例代码
2020/12/04 Vue.js
mustache.js实现首页元件动态渲染的示例代码
2020/12/28 Javascript
Python+Opencv识别两张相似图片
2020/03/23 Python
浅谈使用Python内置函数getattr实现分发模式
2018/01/22 Python
Python实现的栈(Stack)
2018/01/26 Python
python库lxml在linux和WIN系统下的安装
2018/06/24 Python
对Python 3.2 迭代器的next函数实例讲解
2018/10/18 Python
浅谈pyqt5中信号与槽的认识
2019/02/17 Python
python+selenium 简易地疫情信息自动打卡签到功能的实现代码
2020/08/22 Python
python利用google翻译方法实例(翻译字幕文件)
2020/09/21 Python
css3学习之2D转换功能详解
2016/12/23 HTML / CSS
英国在线电子和小工具商店:TecoBuy
2018/10/06 全球购物
社区学习雷锋活动总结
2014/04/25 职场文书
2015年派出所民警工作总结
2015/04/24 职场文书
员工拾金不昧表扬稿
2015/05/05 职场文书
调解书格式范本
2015/05/20 职场文书
小学教师教育随笔
2015/08/14 职场文书
学习型家庭事迹材料(2016精选版)
2016/02/29 职场文书
2019个人半年工作总结
2019/06/21 职场文书
mysql如何配置白名单访问
2021/06/30 MySQL