我想将图像从 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/