python - Error while saving base64 encoded image to the filesystem -


i have tried follow example save base64 encoded image receive in http request, filesystem:

imgdata = re.sub('^data:image/.+;base64,', '', inner_data['output']['image'])  open("imagetosave.png", "wb") fh:     fh.write(base64.decodestring(imgdata)) 

i have printed string i'm trying decode , seems correct.

/9j/4aaqskzjrgabaqaaaqabaad/ [...] /+bax2njpq8daytvirzp7uqbbmgrveg6spf1qyk0bcnkzuf/z 

but keep getting error

typeerror: expected bytes-like object, not str 

the base64.decodestring() function expects bytes, not str object. you'll need encode base64 string bytes first. since characters in such string ascii characters, use codec:

fh.write(base64.decodestring(imgdata.encode('ascii'))) 

from base64.decodestring() documentation:

decode bytes-like object s, must contain 1 or more lines of base64 encoded data, , return decoded bytes.


Comments

Popular posts from this blog

java - SSE Emitter : Manage timeouts and complete() -

jquery - uncaught exception: DataTables Editor - remote hosting of code not allowed -

java - How to resolve error - package com.squareup.okhttp3 doesn't exist? -