Sunday, 15 June 2014

Bytestring to Image in python -



Bytestring to Image in python -

i'm using api retrieves media, api returns media bytestring...i want save bytestring image. how do this?

i've tried this:

data = *bytestring* f = open('image.jpg','w') f.write(str(data)) f.close()

and compiles successfully, when check image.jpg...it empty or "can't opened because unknown format"

try:

f = open('image.jpg','wb') f.write(data) f.close()

why? default, python convert line ending found used underling platform (on windows example, convert '\n' '\n\r'), screw-up file.

it consider files text files unless tell explicitly binary adding 'b' alternative in open(). if uses 'b' alternative not convert line endings.

and syntax aspect, way improve write (it close file in case of exception):

with open('image.jpg','wb') f: f.write(data)

python image bytestring

No comments:

Post a Comment