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

So, I have .jpg / .png, and I opened it in a text editor, which was below:

Anyway, can I save these exotic characters in a string in Python so that I can later write this to a file to create an image?

I tried to import a string that had a beta character, and I received an error message that sends Non-ASCII, so I assume this will happen for this.

Is there any way around this problem?

thanks

A portion of the .png image in a text editor:

enter image description here

+4
source share
2 answers

What you see in your text editing is a binary file that tries to represent all this in human readable characters.

Just open the file as a binary in python:

with open('picture.png', 'rb') as f: data = f.read() with open('picture_out.png', 'wb') as f: f.write(data) 
+18
source

You can read the file in binary format by pointing the rb flag to open , and then just save what ever comes from the file to a text file. I don’t know what is the point of this, but there you go

 # read in image data fh = open('test.png','rb') data = fh.read() fh.close() # write gobbledigoock to text file fh = open('test.txt','w') fh.write(data) fh.close fh.close() 
+1
source

All Articles