我想将图像从QImage或Qpixmap类转换为PIL图像。 我发现这个: 将PyQt转换为PIL图像
但它似乎在Python3中不起作用。 Pyhton3的实现方式还是更简单地使用新方法?
I would like to convert a image from QImage or Qpixmap class to PIL Image. I found this: Convert PyQt to PIL image
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。
所以解决方案是将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()According to the 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.
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()更多推荐
发布评论