Writing stored image to file?

I want to have a python script that creates a new image from the image data that is already embedded in the script itself

Latest efforts:

instead of just pasting the image data into my new script file (because it doesn’t work) ive decided to make a certain python script that reads in the data from the image on my desktop and stores it as a variable called “data”, then writes a new python script and incorporates the “data” variable in with it.

define The original image that resides on my desktop

original = “C:\users\joe\desktop\galaxy.png”

Open the original image and set its data as a variable called “data”

pic = open(original, “rb”)
data = pic.read()
pic.close()

set up the code for the new script which includes “data”

line1 = “image = open(‘image.png’, ‘w’)”
line2 = “image.write(” + “’” + data + “’” + “)”
line3 = “image.close()”

write the new code as a .py

newscript = open(“newscript.py”, “w”)
newscript.write(line1 + "
" + line2 + "
" + line3)
newscript.close()

so the new script, which contains the binary data as “data” looks like this:

image = open(‘image.png’, ‘w’)
image.write(‘all that binary stuff from the image’)
image.close()

but when I run the new script that has the image data stored in it I get this error:

“there is an error in your program
EOL while scanning string literal”

Does this answer your question?

No. Im saying I want to write to a file, the binary image data that is saved as a string variable in my script.py

binary data I pasted into the script from having opened the image with notepad

ahm… that’s not going to work?! You will lose most of the information, as there’s not a printable character for every ascii/unicode letter and text editors usually replace them. Furthermore, to have a string with binary data in it in python, you needed that string to be py-encoded (at least the non-printable chars), e.g. “\x10\x13”

so really…is there no way to do this?

sure there is, but your approach puzzles me a bit…

do you only have that image data pasted into notepad? don’t you have it anywhere else? If the original data is still in the clipboard, use HxD or any hex editor to paste and save to file.

I have the most current version of my effort at the top of this post. Please read

uhm… well of course it fails, 'cause you mix python with binary data without escaping or encoding it. If all you wanna do is to store the content of a file inside a python script .py, do this:

import base64

infile = open(r"D:\folder\binary_doc.pdf", "rb")
outfile = open(r"C:	mp\binary_data_in_script.py", "wb")

outfile.write(b'import base64

')
outfile.write(b'binary_data = "')
outfile.write(base64.b64encode(infile.read()))
outfile.write(b'"

')
outfile.write(b'file = open(r"C:\	mp\\binary_data.pdf", "wb")
')
outfile.write(b'file.write(base64.b64decode(binary_data))
')
outfile.write(b'file.close()
')

infile.close()
outfile.close()

of course, there are other encoding/escaping techniques, but this works out of the box.

what does the b stand for when doing outfile.write(b’string’) etc ?

>>> type("foo")
<class 'str'>

>>> type(b"foo")
<class 'bytes'>

I open the file in binary mode, this was actually for testing. You could also use strings in acsii mode.

oh. and what does the r mean for open(r"myfile.pdf", “wb”)? what does it do?

It makes it a raw string, you can have unescaped backslashes in it.

http://docs.python.org/3/reference/lexical_analysis.html#literals

Thanks so much man. Also, using python 2.6 as i have been, i have tweeked your given code that runs without having to use ‘r’ or ‘b’. Is there any reason why the below code wouldnt be as good as the original code you shared with me?

import base64

infile = open(“C:\users\arya\desktop\Binary IO\galaxy.png”, “rb”)
outfile = open(“NewScript.py”, “wb”)

outfile.write('import base64

‘)
outfile.write(‘binary_data = "’)
outfile.write(base64.b64encode(infile.read()))
outfile.write(’"

')
outfile.write('file = open(“NewImage.png”, “wb”)
')
outfile.write('file.write(base64.b64decode(binary_data))
')
outfile.write('file.close()
')

infile.close()
outfile.close()

i guess it’s fine, raw strings are just for convenience and byte file writing wasn’t necessary at all due to base64 encoding.

haha yes!!! thank you for helping me codemanx!!! really appreciated!!!