【发布时间】:2019-06-10 20:06:13
【问题描述】:
我使用 PIL 进行图像拼接,并希望使用 QGraphicsView 查看结果。为此,我对QtImageViewer 进行了子类化。相关方法有
def setImage(self, image):
""" Set the scene's current image pixmap to the input QImage or QPixmap.
Raises a RuntimeError if the input image has type other than QImage or QPixmap.
:type image: QImage | QPixmap
"""
# image.save("r:/test.png")
if isinstance(image, QPixmap):
pixmap = image
elif isinstance(image, QImage):
pixmap = QPixmap.fromImage(image)
else:
raise RuntimeError("ImageViewer.setImage: Argument must be a QImage or QPixmap.")
if self.hasImage():
self._pixmapHandle.setPixmap(pixmap)
else:
self._pixmapHandle = self.scene.addPixmap(pixmap)
self.setSceneRect(QRectF(pixmap.rect())) # Set scene size to image size.
self.updateViewer()
我从它的子类中调用如下:
def redraw(self):
img = Image.new("RGB", (self.width(), self.height()), "gray")
if self._source is not None:
self._source.paintMap(img)
img.save("r:/debug.png")
# self.setImage(QImage("r:\debug.png"))
self.setImage(ImageQt(img))
在两个 sn-ps 中都有几行测试代码,将图像内容保存到临时文件中。当我保存 PIL 图像 (img.save("r:/debug.png")) 并从该文件 (self.setImage( QImage("r:\debug.png"))) 则图像显示正确。
问题
现在可以观察到三种情况:
- 只运行代码(没有测试代码):显示了一个图形,但显然它是一块随机内存,虽然看起来总是一样的。
- 调试代码,在setImage()中停止,保存传入的图片:保存的文件和显示的图片都是正确的。
- debug or run with the line image.save("r:/test.png") uncommented: then python crashs.
所以,我认为 ImageQt 中有些东西已经烂了,最终可能会得到纠正。奇怪的行为让人想起了Nick Morgan discusses 的问题,但只是将 imageQt 包装在另一个 QImage 中并不会改变这种情况。哦,是的,我在 Windows 下运行。
进一步:与PIL Image to QPixmap conversion issue 没有任何联系,因为该问题与颜色通道的顺序有关,与 imageQt 中的虚假内存指针无关。
问题
是否有可用的替代转换方法(保存和读取作为解决方法?)?
能否推动 imageQt 表现得像它应该的那样(或者我自己做错了什么)?
【问题讨论】:
-
我不明白你的问题,为什么你需要保存文件才能读回来?,从长远来看,这会带来一些不便,因为很多时候你不想保存到硬盘驱动器由于各种原因。将 PIL 图像转换为 QImage 时遇到问题吗? ImageQt 有什么问题?提供minimal reproducible example
-
如上所述,那部分是测试代码。我在这里明确提到它是为了表明它本身的图像很好并且可以正确显示。应该没有必要保存/读取图像,从而减慢程序的速度,但我不同意这本身就是一个坏主意,只要它是在适当的临时文件夹中完成的。
-
我明白这一点,但仍然 1) 你的问题是什么?您没有解释清楚,似乎您假设我们知道您的问题,2)您必须提供MCVE
-
如果您甚至不理解问题,如何将其标记为重复,这让我感到困惑。你在这里几乎完全错了。
-
因为您的答案相当于说“使用其他答案”,所以当您完成上一版(图片)时,我也理解了您的问题,但我看到的答案指向另一个等效的答案复制。
标签: python pyqt python-imaging-library qimage