Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 873
O.rka Avatar asked Aug 24 '12 20:08

O.rka


People also ask

How do you write to PNG in Python?

The basic strategy is to create a Writer object (instance of png. Writer ) and then call its png. write() method with an open (binary) file, and the pixel data. The Writer object encapsulates all the information about the PNG file: image size, colour, bit depth, and so on.

Does Python support PNG?

In python we use a library called PIL (python imaging Library). The modules in this library is used for image processing and has support for many file formats like png, jpg, bmp, gif etc.

How do you write to an image in Python?

The save() function writes an image to file. Like for reading (open() function), the save() function accepts a filename, a path object or a file object that has been opened to write.


1 Answers

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)
like image 197
jdi Avatar answered Sep 24 '22 06:09

jdi