Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 如何从SQL Server数据库中提取字节并在Python中转换为图像_Python 3.x_Byte_Pypyodbc_Python Bytearray - Fatal编程技术网

Python 3.x 如何从SQL Server数据库中提取字节并在Python中转换为图像

Python 3.x 如何从SQL Server数据库中提取字节并在Python中转换为图像,python-3.x,byte,pypyodbc,python-bytearray,Python 3.x,Byte,Pypyodbc,Python Bytearray,-我正在使用python将测试数据加载到我的SQL Server数据库中,并且能够成功地拍摄图像并将其分解为字节并存储在数据库中,但是当尝试取回字节并对其进行解码以将其保存为新的文件类型时,我得到的只是一个空白图像文件。我不知道我做错了什么 -我尝试过使用其他教程中的base64和类似问题进行多次迭代,但似乎找不到解决我问题的方法 SQLCommand = ("SELECT Photo FROM Validation") cursor.execute(SQLCommand) data = c

-我正在使用python将测试数据加载到我的SQL Server数据库中,并且能够成功地拍摄图像并将其分解为字节并存储在数据库中,但是当尝试取回字节并对其进行解码以将其保存为新的文件类型时,我得到的只是一个空白图像文件。我不知道我做错了什么

-我尝试过使用其他教程中的base64和类似问题进行多次迭代,但似乎找不到解决我问题的方法

SQLCommand = ("SELECT Photo FROM Validation")


cursor.execute(SQLCommand)
data = cursor.fetchone()[0]




image_64_decode = base64.decodebytes(data)
image_result = open('booking.png', 'wb')
image_result.write(image_64_decode)
image_result.close()


connection.close()

The expected result is that I should be able to fetch the bytes from the database which the database column is varbinary(max) the equivalent of bytes in python. once the bytes are fetched using the script in python it should save a file as booking.png which should replicate the image i stored in the database.

When i run the script i don't get an error, and in fact it saves a file, but the file is empty containing 1kb and does not reproduce the image. Not sure where i am going wrong, but it seems like it's not properly fetching the bytes.

我能够让代码正常工作,并且能够成功地将图像转换为字节,将其存储在sql server数据库中,并通过获取字节和复制图像来检索它

只有一个问题-只有在存储图像字节的列使用nvarcharmax数据类型时,这才有效。当我使用varbinarymax或者当我解决错误时,我会遇到错误,它实际上并没有获取比特并正确地转换它——任何关于我可能会做错什么的指导,因为我感觉它很小。下面更新的代码是我正在使用正在工作的nvarcharmax所做的

import pypyodbc
import base64
from base64 import * 

connection = pypyodbc.connect('Driver=SQL Server;'
                            'Server=DESKTOP-MSSQLSERVER;'
                            'Database=Test;'
                            'Trusted_Connection=yes;'
                            )

cursor = connection.cursor()

a = 'bob@bob.com'
b = 'mack jones'
filename = 'bookingsuccessful.PNG'


image = open(filename, 'rb')
image_read = image.read()
image_64_encode = base64.encodebytes(image_read)


image.close()

SQLCommand = ("INSERT INTO Validation(email, myname, photo) VALUES(?,?,?)")
Values = [a,b,image_64_encode]
cursor.execute(SQLCommand, Values)
connection.commit()

SQLCommand = ("SELECT Photo FROM validation")
cursor.execute(SQLCommand)
data = cursor.fetchone()[0]
data = bytes(data.strip("\n"), 'utf-8')

image_64_decode = base64.decodebytes(data)
image_result = open('testfile.gif', 'wb')
image_result.write(image_64_decode)
image_result.close()

connection.close()

实际上不需要base64编码。如果您使用而不是pyodbc,那么它与

测试数据 photo\u path=r'C:\Users\Public\Pictures'+'\\' 电子邮件bob@example.com' 测试环境 cursor.execute\ 创建表验证 电子邮件nvarchar255主键, 照片varbinarymax 保存二进制文件 使用openphoto_path+“generic_man.jpg”、“rb”作为照片_文件: photo_bytes=photo_file.read cursor.executeINSERT进入验证电子邮件、照片值?、?电子邮件、照片字节 printf'{lenphoto_bytes}-为{email}编写的字节文件 写入的5632字节文件bob@example.com 检索二进制数据并另存为新文件 已检索_bytes=cursor.executes从验证中选择照片,其中email=?,email.fetchval 使用openphoto_path+'new.jpg','wb'作为new_jpg: 新建_jpg.writeretrieved_字节 printf“{lenretrieved_bytes}检索并写入新文件的字节” 检索5632字节并写入新文件
Base64编码将字节序列转换为ASCII字符序列。如果以base64编码形式存储二进制文件,则应使用varcharmax列,以字符串形式检索编码文件,然后对其进行解码。如果要将文件数据作为原始字节存储在varbinarymax列中,则不要对其进行base64编码。您好,Gord-感谢您的评论。作为后续问题,我肯定可以尝试一下,看看它是否适合我的需要,因为我没有想过不解码,特别是因为这是我第一次使用base64,但根据您的经验,使用base64编码与存储原始字节是否有好处。是否存在存储或性能增益,或其他?是否存在存储或性能增益[通过使用base64编码]——恰恰相反。编码将3个原始字节转换为4个ASCII字符,每个ASCII字符本身将消耗一个字节,因此编码将增加至少33%的存储需求,例如,3MB文件将产生至少4MB的字符串。还需要在保存数据之前对其进行编码,并在检索数据之后对其进行解码。