将base64编码图像解码为python中的orignal图像

时间:2018-06-13 07:15:23

标签: python python-3.x python-2.7 base64 pymysql

我从远程数据库中拉出base64编码的图像字符串并尝试使用base64.decode()解码它,但输出图像是损坏的(图像大小只有几个字节甚至0个字节) 代码是:

import pymssql
import base64
import os

con=pymssql.connect(user='**',
                    password='**',
                    database='**',
                    host='**')

cur=con.cursor()
cur.execute("SELECT PHOTOGRAPH_IMG FROM IHHL_DETAIL_IMG WHERE APPLICATION_ID='**';")
result=cur.fetchall()  #tried fetchone() too
result=list(result)    #to save the tuple returned as a list
print result[0]        

cur.close()
con.close()

#I've tried various formats to decode, none work 
#fh=open("img.jpeg","wb")
#fh.write(result[0].decode('base64'))
with open("imageToSave.png", "wb") as fh:
fh.write(base64.decode(result[0]))
fh.close()

1 个答案:

答案 0 :(得分:0)

只需将其添加为“代码”

即可
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA