简体   繁体   English

在python中将base64编码的图像解码为原始图像

[英]decoding a base64 encoded image into the orignal image in python

I Pulled a base64 encoded image string from a remote db and tried decoding it using base64.decode() but the output image is a corrupt one (image size is just a few Bytes or even 0 bytes) The code is: 我从远程数据库中提取了一个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()

只需将其添加为“代码”

AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM