python - 如何在 Python 3 中将 QImage(QPixmap) 转换为 PIL Image?

标签 python pyqt pyqt4 python-imaging-library pyqt5

我想将图像从 QImage 或 Qpixmap 类转换为 PIL Image。
我发现了这个:Convert PyQt to PIL image

但它似乎在 Python3 中不起作用。是否存在将其实现到 Pyhton3 或更简单地使用新方法的方法?

最佳答案

根据docs :

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 .

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 - 如何在 Python 3 中将 QImage(QPixmap) 转换为 PIL Image?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47289884/

相关文章:

Python - 使用重新采样而不使用平均值/均值进行下采样

python - 从 PyQt4 ListWidgetItem 获取所有数据

python - PyQt4:获取QListWidget中所有标签的列表

python - 有没有办法使用Python在Windows上通过PID或名称获取进程信息?

Python正则表达式匹配任何顺序

python - psycopg2.OperationalError : FATAL: password authentication failed for user "<my UNIX user>"

python - pylint 错误在 python3.2 中运行正常,但在 python3.3 中失败

multithreading - 为什么 Pycharm 调试器不会在主线程外的断点处停止?

python - PYQT:如何捕获python解释器的输出并将其显示在QEditText中?

python - 创建父级后如何动态添加小部件