简体   繁体   中英

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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