Python pymysql:如何从数据库加载图像?

Python pymysql: How to load an image from database?

我想在 PyQt5 window 中从我的数据库加载和显示图像。 但是我的脚本不起作用。

    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()

我应该怎么做才能显示图像?

load 方法仅采用文件路径或 IODevice,而您似乎以字节形式传入原始图像数据。

试试这个,而不是:

image = QImage.fromData(result[0]['Image'])

要保存图像,请执行以下操作:

image.save('image.png')