I want to load and display an image from my database within a PyQt5 window. But my script doesn't work.
connection = None
result = None
try:
connection = pymysql.connect(host = 'localhost',
user = 'root',
db = 'mydatabase',
cursorclass = pymysql.cursors.DictCursor)
try:
with connection.cursor() as cursor:
sql = "SELECT image FROM mydatabase"
cursor.execute(sql)
result = cursor.fetchall() #Get all values from the image column
image = QImage()
image.load(result[0]['Image']) #Load image from the first row
label = QLabel()
label.setPixmap(QPixmap.fromImage(image))
self.grid.addWidget(label, 0, 0)
finally:
connection.close()
What should I do to display the image?
答案 0 :(得分:2)
load
方法只接受文件路径或IODevice
,而您似乎将原始图像数据作为字节传递。
试试这个,而不是:
image = QImage.fromData(result[0]['Image'])
要保存图像,请执行以下操作:
image.save('image.png')