我如何将从数据库接收到的 blob 数据转换为 python 中的图像

How can i convert blob data received from database to image in python

我从数据库接收到 longblob 数据。

我尝试将 blob 数据转换为图像并使用 cv2 读取图像。

所以我尝试像下面的代码一样将 blob 数据转换为 base64,但失败了。

img = base64.decodebytes(img_str)

如何将 blob 转换为图像? cv2 包中是否有针对此问题的转换功能?

您不需要 cv2 将 blob 转换为图像,您需要将 blob/image 存储在磁盘上并显示它。此处和从 mysql blob 检索到磁盘文件的示例..

祝你好运!

第URL页被引用:URL

import mysql.connector
from mysql.connector import Error

def write_file(data, filename):
    # Convert binary data to proper format and write it on Hard Disk
    with open(filename, 'wb') as file:
        file.write(data)

def readBLOB(emp_id, photo, bioData):
    print("Reading BLOB data from python_employee table")

    try:
        connection = mysql.connector.connect(host='localhost',
                                             database='python_db',
                                             user='pynative',
                                             password='pynative@#29')

        cursor = connection.cursor()
        sql_fetch_blob_query = """SELECT photo from python_employee where id = %s"""

        cursor.execute(sql_fetch_blob_query, (emp_id,))
        record = cursor.fetchall()
        for row in record:
            print("Id = ", row[0], )
            print("Name = ", row[1])
            image = row[2]
            file = row[3]
            print("Storing employee image and bio-data on disk \n")
            write_file(image, photo)
            write_file(file, bioData)

    except mysql.connector.Error as error:
        print("Failed to read BLOB data from MySQL table {}".format(error))

    finally:
        if (connection.is_connected()):
            cursor.close()
            connection.close()
            print("MySQL connection is closed")

readBLOB(1, "D:\Python\Articles\my_SQL\query_output\eric_photo.png",
         "D:\Python\Articles\my_SQL\query_output\eric_bioData.txt")
readBLOB(2, "D:\Python\Articles\my_SQL\query_output\scott_photo.png",
         "D:\Python\Articles\my_SQL\query_output\scott_bioData.txt")

如果要保存图像,请使用 'Danilo Mercado Oudalova' 中的代码。

但是如果你想在没有保存文件的情况下使用,使用下面的例子。

import mysql.connector
from mysql.connector import Error
from io import BytesIO #from io import StringIO.
import PIL.Image



try:
    connection = mysql.connector.connect(host='localhost',
                                         database='python_db',
                                         user='pynative',
                                         password='pynative@#29')

    cursor = connection.cursor()
    sql = "your query"

    cursor.execute(sql)
    result = cursor.fetchall()

except mysql.connector.Error as error:
    print("Failed to read BLOB data from MySQL table {}".format(error))

finally:
    if (connection.is_connected()):
        cursor.close()
        connection.close()
        print("MySQL connection is closed")

type(result[0][0])
#If the type is byte, use from io import BytesIOf. If str, use from io import StringIO.
file_like= BytesIO(result[0][0])
img=PIL.Image.open(file_like)
img.show()