"Segmentation fault (core dumped)" 当图像来自 Pillow 时尝试将 Qpixmap 设置为 Qlabel

"Segmentation fault (core dumped)" when trying to set a Qpixmap to Qlabel when image comes from Pillow

所以我正在尝试在我的 UI 上显示图像。我发现这样做的方法是将 QPixmap 设置为 Qlabel。

qimg = QtGui.QImage("Lenna.png") # load QImage
pixImg = QtGui.QPixmap().fromImage(qimg) # convert QImage to QPixmap
self.imgLabel.setPixmap(pixImg) # add the QPixmap picture to GUI

这工作得很好,当我使用 Pillow 获取 QImage qimg = PIL.Image.open("Lenna.png").toqimage() 时出现问题,那就是当我遇到段错误时。我试过将任何枕头给我的东西投射到 QImage 中,但这并没有解决任何问题,而且我找不到其他可能有点相关的东西

PIL.ImageQt 有一个非常简洁的 toqpixmap(img) 方法,由于某种原因没有在文档中。因此,我没有进行冗长的转换(当时我认为这是唯一的方法),而是可以

img = Image.open("Lenna.png")
pixImg = ImageQt.toqpixmap(img)
self.imgLabel.setPixmap(pixImg)

这似乎工作得很好