Python生态圈图像格式转换问题(推荐)


Posted in Python onDecember 02, 2019

在Python生态圈里,最常用的图像库是PIL——尽管已经被后来的pillow取代,但因为pillow的API几乎完全继承了PIL,所以大家还是约定俗成地称其为PIL。除PIL之外,越来越多的程序员习惯使用openCV来处理图像。另外,在GUI库中,也有各自定义的图像处理机制,比如wxPyton,定义了wx.Image做为图像处理类,定义了wx.Bitmap做为图像显示类。

下图梳理出了PIL读写图像文件、cv2读写图像文件、PIL对象和cv2对象互转、PIL对象和wx.Image对象互转、以及numpy数组转存图像的方法。掌握了这些方法,足可应对各种各样的图像处理需求了。

Python生态圈图像格式转换问题(推荐)

1. PIL读写图像文件

下面的代码,演示了用PIL读取png格式的图像文件,剔除alpha通道后转存为jpg格式的图像文件。

>>> from PIL import Image
>>> im = Image.open(r'D:\CSDN\Python_Programming.png')
>>> r,g,b,a = im.split()
>>> im = Image.merge("RGB",(r,g,b))
>>> im.save(r'D:\CSDN\Python_Programming.jpg')

2. cv2读写图像文件

下面的代码,演示了用cv2读取png格式的图像文件,转存为jpg格式的图像文件。

>>> import cv2
>>> im = cv2.imread(r'D:\CSDN\Python_Programming.png')
>>> cv2.imwrite(r'D:\CSDN\Python_Programming.jpg', im)
True

3. PIL对象和cv2对象互转

cv2格式的对象,本质上就是numpy数组,也就是numpy.ndarray对象。只要能做到PIL对象和numpy数组互转,自然就实现了PIL对象和cv2对象互转。

下面的代码,演示了用PIL读取png格式的图像文件,转成numpy数组后保存为图像文件。

>>> import cv2
>>> from PIL import Image
>>> import numpy as np
>>> im_pil = Image.open(r'D:\CSDN\Python_Programming.png')
>>> im_cv2 = np.array(im_pil)
>>> cv2.imwrite(r'D:\CSDN\Python_Programming.jpg', im_cv2)
True

下面的代码,用cv2读取png格式的图像文件,转成PIL对象后保存为图像文件。

>>> import cv2
>>> from PIL import Image
>>> im_cv2 = cv2.imread(r'D:\CSDN\Python_Programming.png')
>>> im_pil = Image.fromarray(im_cv2)
>>> im_pil.save(r'D:\CSDN\Python_Programming.jpg')

4. PIL对象和wx.Image对象互转

这是实现PIL对象和wx.Image对象互转的两个函数。

def PilImg2WxImg(pilImg):
  '''PIL的image转化为wxImage'''
  image = wx.EmptyImage(pilImg.size[0],pilImg.size[1])
  image.SetData(pilImg.convert("RGB").tostring())
  image.SetAlphaData(pilImg.convert("RGBA").tostring()[3::4])
  return image
def WxImg2PilImg(wxImg):
  '''wxImage转化为PIL的image'''
  pilImage = Image.new('RGB', (wxImg.GetWidth(), wxImg.GetHeight()))
  pilImage.fromstring(wxImg.GetData())
  if wxImg.HasAlpha():
    pilImage.convert( 'RGBA' )
    wxAlphaStr = wxImg.GetAlphaData()
    pilAlphaImage = Image.fromstring( 'L', (wxImg.GetWidth(), wxImg.GetHeight()), wxAlphaStr )
    pilImage.putalpha( pilAlphaImage )
  return pilImage

5. numpy数组转存图像

下面的代码,生成了一张515x512像素的随机图像。

>>> from PIL import Image
>>> import numpy as np
>>> a = np.random.randint(0,256,((512,512,3)), dtype=np.uint8)
>>> im_pil = Image.fromarray(a)
>>> im_pil.save(r'D:\CSDN\random.jpg')

Python生态圈图像格式转换问题(推荐)

总结

以上所述是小编给大家介绍的Python生态圈图像格式转换问题,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对三水点靠木网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

Python 相关文章推荐
Python列表list数组array用法实例解析
Oct 28 Python
Python实现检测服务器是否可以ping通的2种方法
Jan 01 Python
Python 性能优化技巧总结
Nov 01 Python
PyQt5每天必学之QSplitter实现窗口分隔
Apr 19 Python
python控制windows剪贴板,向剪贴板中写入图片的实例
May 31 Python
python实现定时发送qq消息
Jan 18 Python
python实现的生成word文档功能示例
Aug 23 Python
使用Python实现分别输出每个数组
Dec 06 Python
Pytorch实现的手写数字mnist识别功能完整示例
Dec 13 Python
解决tensorflow添加ptb库的问题
Feb 10 Python
Python读取excel文件中带公式的值的实现
Apr 17 Python
解决pycharm安装第三方库失败的问题
May 09 Python
python 申请内存空间,用于创建多维数组的实例
Dec 02 #Python
python将数组n等分的实例
Dec 02 #Python
python实现自动化报表功能(Oracle/plsql/Excel/多线程)
Dec 02 #Python
Python中类似于jquery的pyquery库用法分析
Dec 02 #Python
python 检查数据中是否有缺失值,删除缺失值的方式
Dec 02 #Python
python实现两个字典合并,两个list合并
Dec 02 #Python
Python:合并两个numpy矩阵的实现
Dec 02 #Python
You might like
php正则表达匹配中文问题分析小结
2012/03/25 PHP
详解阿里云视频直播PHP-SDK接入教程
2020/07/09 PHP
childNodes.length与children.length的区别
2009/05/14 Javascript
IE8下关于querySelectorAll()的问题
2010/05/13 Javascript
jquery判断字符输入个数(数字英文长度记为1,中文记为2,超过长度自动截取)
2010/10/15 Javascript
JavaScript版DateAdd和DateDiff函数代码
2012/03/01 Javascript
jquery 实现二级/三级/多级联动菜单的思路及代码
2013/04/08 Javascript
jQuery.holdReady()使用方法
2014/05/20 Javascript
Javascript的闭包详解
2014/12/26 Javascript
Bootstrap布局组件应用实例讲解
2016/02/17 Javascript
JS运动改变单物体透明度的方法分析
2018/01/23 Javascript
详解Vue.js中.native修饰符
2018/04/24 Javascript
微信小程序前端自定义分享的实现方法
2019/06/13 Javascript
js实现简单的无缝轮播效果
2020/09/05 Javascript
vue 解决IOS10低版本白屏的问题
2020/11/17 Javascript
[03:58]兄弟们,回来开黑了!DOTA2昔日战友招募宣传视频
2016/07/17 DOTA
Python 异常处理实例详解
2014/03/12 Python
介绍Python中几个常用的类方法
2015/04/08 Python
Python实现分割文件及合并文件的方法
2015/07/10 Python
Python之py2exe打包工具详解
2017/06/14 Python
Python通过OpenCV的findContours获取轮廓并切割实例
2018/01/05 Python
Python 使用with上下文实现计时功能
2018/03/09 Python
Django自定义用户认证示例详解
2018/03/14 Python
python中dict字典的查询键值对 遍历 排序 创建 访问 更新 删除基础操作方法
2018/09/13 Python
Python中反射和描述器总结
2018/09/23 Python
Django 路由控制的实现
2019/07/17 Python
Python如何执行精确的浮点数运算
2020/07/31 Python
综合测评自我鉴定
2013/10/08 职场文书
互联网电子商务专业毕业生求职信
2014/03/18 职场文书
加强作风建设心得体会
2014/10/22 职场文书
2014年团队工作总结
2014/11/24 职场文书
领导欢迎词范文
2015/01/26 职场文书
警告通知
2015/04/25 职场文书
绿里奇迹观后感
2015/06/15 职场文书
毕业酒会致辞
2015/07/29 职场文书
Python turtle实现贪吃蛇游戏
2021/06/18 Python