Python从PostgreSQL中保存和恢复Image/Pictures/JPEG

2024-04-18 14:25:40 发布

您现在位置:Python中文网/ 问答频道 /正文

所以,我尝试使用psycopg2在Python中将图像保存到PostgreSQL表中

插入查询(插入.py)在

#Folder/Picture is my path/ID is generated/IDnum is increment
picopen = open("Folder/Picture."+str(ID)+"."+str(IDnum)+".jpg", 'rb').read()
filename = ("Picture."+str(ID)+"."+str(IDnum)+".jpg")

#Sample file name is this Picture.1116578795.7.jpg

#upload_name is where I want the file name, upload_content is where I store the image
SQL = "INSERT INTO tbl_upload (upload_name, upload_content) VALUES (%s, %s)"
data = (filename, psycopg2.Binary(picopen))
cur.execute(SQL, data)
conn.commit()

现在要恢复保存的图像,我执行这个查询(恢复.py)在

^{pr2}$

当我执行恢复.py它确实生成了一个“.jpg”文件,但我无法查看或打开它。在

如果有帮助的话,我会用python2.7和Centos7来做。 为了获得更多的信息,当我打开生成的文件时,我在图像查看器上得到这个。在

Error interpreting JPEG image file (Not a JPEG file: starts with 0x5c 0x78)

我也尝试过使用其他格式(.png,.bmp)


Tags: namepy图像idisfolderfilenamepsycopg2