使用 PyQt5 QLabel.setPixmap 后出现分段错误
Segmentation fault after QLabel.setPixmap with PyQt5
我正在尝试使用 QPixmap 和 QLabel 显示 PIL 图像。但是当我 运行 代码时,我得到了 SIGSEGV。
代码:
import sys
from PIL import Image
from PIL.ImageQt import ImageQt
from PyQt5 import QtWidgets
from PyQt5.QtGui import QPixmap
from PyQt5.QtWidgets import QApplication, QMainWindow
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setupUi(self)
# im = Image.new('RGB', (200, 200), (255, 255, 255))
im = Image.new('RGB', (500, 500), (255, 255, 255))
self.label.setPixmap(QPixmap.fromImage(ImageQt(im)))
def setupUi(self, MainWindow):
MainWindow.resize(767, 557)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.layout = QtWidgets.QGridLayout(self.centralwidget)
self.label = QtWidgets.QLabel(self.centralwidget)
self.layout.addWidget(self.label, 0, 0, 1, 1)
MainWindow.setCentralWidget(self.centralwidget)
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = MainWindow()
ex.show()
sys.exit(app.exec_())
当我将图像大小更改为 200x200 时,它不会抛出分割错误但会随机着色像素。
如何将我的 PIL 图像正确地放入 window?
尝试 RGBA
格式:
import sys
from PIL import Image
from PIL.ImageQt import ImageQt
from PyQt5 import QtWidgets
from PyQt5.QtGui import QPixmap
from PyQt5.QtWidgets import QApplication, QMainWindow
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setupUi(self)
# im = Image.new('RGB', (200, 200), (255, 255, 155))
# im = Image.new('RGB', (500, 500), (255, 255, 255))
im = Image.new("RGBA", (500, 500), (255, 155, 155, 255)) # <---
self.label.setPixmap(QPixmap.fromImage(ImageQt(im)))
def setupUi(self, MainWindow):
MainWindow.resize(767, 557)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.layout = QtWidgets.QGridLayout(self.centralwidget)
self.label = QtWidgets.QLabel(self.centralwidget)
self.layout.addWidget(self.label, 0, 0, 1, 1)
MainWindow.setCentralWidget(self.centralwidget)
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = MainWindow()
ex.show()
sys.exit(app.exec_())
我正在尝试使用 QPixmap 和 QLabel 显示 PIL 图像。但是当我 运行 代码时,我得到了 SIGSEGV。 代码:
import sys
from PIL import Image
from PIL.ImageQt import ImageQt
from PyQt5 import QtWidgets
from PyQt5.QtGui import QPixmap
from PyQt5.QtWidgets import QApplication, QMainWindow
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setupUi(self)
# im = Image.new('RGB', (200, 200), (255, 255, 255))
im = Image.new('RGB', (500, 500), (255, 255, 255))
self.label.setPixmap(QPixmap.fromImage(ImageQt(im)))
def setupUi(self, MainWindow):
MainWindow.resize(767, 557)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.layout = QtWidgets.QGridLayout(self.centralwidget)
self.label = QtWidgets.QLabel(self.centralwidget)
self.layout.addWidget(self.label, 0, 0, 1, 1)
MainWindow.setCentralWidget(self.centralwidget)
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = MainWindow()
ex.show()
sys.exit(app.exec_())
当我将图像大小更改为 200x200 时,它不会抛出分割错误但会随机着色像素。
如何将我的 PIL 图像正确地放入 window?
尝试 RGBA
格式:
import sys
from PIL import Image
from PIL.ImageQt import ImageQt
from PyQt5 import QtWidgets
from PyQt5.QtGui import QPixmap
from PyQt5.QtWidgets import QApplication, QMainWindow
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setupUi(self)
# im = Image.new('RGB', (200, 200), (255, 255, 155))
# im = Image.new('RGB', (500, 500), (255, 255, 255))
im = Image.new("RGBA", (500, 500), (255, 155, 155, 255)) # <---
self.label.setPixmap(QPixmap.fromImage(ImageQt(im)))
def setupUi(self, MainWindow):
MainWindow.resize(767, 557)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.layout = QtWidgets.QGridLayout(self.centralwidget)
self.label = QtWidgets.QLabel(self.centralwidget)
self.layout.addWidget(self.label, 0, 0, 1, 1)
MainWindow.setCentralWidget(self.centralwidget)
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = MainWindow()
ex.show()
sys.exit(app.exec_())