如何在Python 3中将QImage(QPixmap)转换为PIL图像? [英] How to convert QImage(QPixmap) to PIL Image in Python 3?

查看:1158
本文介绍了如何在Python 3中将QImage(QPixmap)转换为PIL图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将图像从QImage或Qpixmap类转换为PIL图像. 我发现了这一点:将PyQt转换为PIL图像

I would like to convert a image from QImage or Qpixmap class to PIL Image. I found this: Convert PyQt to PIL image

但是它似乎在Python3中不起作用.是将其实现到Pyhton3还是更简单地使用新方法来实现?

But it seems to doesn't work in Python3. Is the way to implement this to Pyhton3 or do it more simply using new approaches exist?

推荐答案

根据文档:

StringIO和cStringIO模块不见了.而是导入io 模块并使用io.StringIO或io.BytesIO来存储文本和数据 分别.

The StringIO and cStringIO modules are gone. Instead, import the io module and use io.StringIO or io.BytesIO for text and data respectively.

因此解决方案是将cStringIO.StringIO转换为io.BytesIO.

So the solution is to do the conversion of cStringIO.StringIO to io.BytesIO.

PyQt5:

import io
from PIL import Image
from PyQt5.QtGui import QImage
from PyQt5.QtCore import QBuffer

img = QImage("image.png")
buffer = QBuffer()
buffer.open(QBuffer.ReadWrite)
img.save(buffer, "PNG")
pil_im = Image.open(io.BytesIO(buffer.data()))
pil_im.show()

PyQt4:

import io
from PIL import Image
from PyQt4.QtGui import QImage
from PyQt4.QtCore import QBuffer

img = QImage("image.png")
buffer = QBuffer()
buffer.open(QBuffer.ReadWrite)
img.save(buffer, "PNG")
pil_im = Image.open(io.BytesIO(buffer.data()))
pil_im.show()

这篇关于如何在Python 3中将QImage(QPixmap)转换为PIL图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆