Python 2.7.3 . . . Write .jpg/.png image file?

O.rka picture O.rka · Aug 24, 2012 · Viewed 54.2k times · Source

So I have a .jpg/.png and I opened it up in Text Edit which I provided below:

Is there anyway I can save these exotic symbols to a string in Python to later write that to a file to produce an image?

I tried to import a string that had the beta symbol in it and I got an error that send Non-ASCII so I am assuming the same would happen for this.

Is there anyway to get around this problem?

Thanks

Portion of Image.png in Text Edit:

enter image description here

Answer

jdi picture jdi · Aug 24, 2012

What you are looking at in your text edit is a binary file, trying to represent it all in human readable characters.

Just open the file as binary in python:

with open('picture.png', 'rb') as f:
    data = f.read()

with open('picture_out.png', 'wb') as f:
    f.write(data)