python提取blob数据_如何将从数据库接收到的blob数据转换成python中的图像

本文介绍如何使用Python从MySQL数据库中检索blob数据,并将其转换为图像文件存储在磁盘上。示例代码展示了如何通过`mysql.connector`连接数据库,执行SQL查询,然后将blob数据写入到PNG图像文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

将blob/image转换为image不需要cv2,只需要将blob/image存储在磁盘上并显示出来。下面是从mysql blob到磁盘文件的检索示例。。在

祝你好运!在

引用的页面URL:URLimport 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]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值