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

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:

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)

You can read to file in binary format by providing the rb flag to open and then just save what ever comes out of the file into a text file. I don't know what the point of this would be 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()

Related

How to create and use a text file in Python?

I need to create a text file in Python to store certain data from a game. I do not want to use numpy, or any external libraries if at all possible.
I need to put some numerical data. Do text files require string data? Also does the data come out of the file as a string?
I know how to create and open a text file, and how to convert string to integer and vice versa, as well as handle CSV file data. I do not know how to handle a text file.
Any ideas on what to do?
To create a file:
file = open("textfile.txt","w+")
This will create a file if it doesn't exist in the directory.
To write inside it:
file.write("This is the content of the file.")
And then you'll have to close the instance with
file.close()
by using the with open command you can create and use it
here is an example
Here w is for writing mode
with open('test.txt','w') as d:
d.write('your text goes here')
You can write to file like this if the file not exists then it will be created
Any ideas on what to do?
Put your data into dict and use built-in json module, example:
import json
data = {'gold': 500, 'name': 'xyzzy'}
# writing
with open('save.json', 'w') as f:
json.dump(data, f)
# reading
with open('save.json', 'r') as f:
data2 = json.load(f)
This create human-readable text file.

File to string conversion in python

dictionary = file . read()
I'm currently creating a cipher solver for the 2017 cipher challenge
I have a word document of fifty eight thousand words but i cannot get the file as a string in python 2.7.9
I have tried many thing i have read online such as the above code but to no avail.
I also need this to be easy to understand as i am new to python
Thanks!Don't be negative be constructive!
The word are from:
http://www.mieliestronk.com/corncob_lowercase.txt
You probably should consult some code examples on the web for reading a file. You need something like:
fp = open(fname, "r")
lines = fp.readlines()
for line in lines:
do_something_with_the_lines
fp.close()
All you have to do is:
with open("dictionary.txt") as f: # Open the file and save it as "f"
dictionary = f.read() # Read the content of the file and save it to "dictionary"
If you want to read it from a website, try this:
import urllib2
dictionary = urllib2.urlopen("http://www.mieliestronk.com/corncob_lowercase.txt").read() # Open the website from the url and save its contents to "dictionary"
I think you should check this out for what you're trying to do (http://www.pythonforbeginners.com/files/reading-and-writing-files-in-python)
This should be helpful

Specific codes for a dat file that contains bytes (Python)

I have another doubt related to reading the dat file.
The file format is DAT file (.dat)
The content inside the file is in bytes.
When I tried the run open file code, the program built and ran successfully. However, the python shell has no output (I can't see the contents from the file).
Since the content inside the file is in bytes, should I modify the code ? What is the code to use for bytes?
Thank you.
There is no "DAT" file format and, as you say, the file contains bytes - as do all files.
It's possible that the file contains binary data for which it's best to open the file in binary mode. You do that by specifying b as part of the mode parameter to open(), like this:
f = open('file.dat', 'rb')
data = f.read() # read the entire file into data
print(data)
f.close()
Note that the full mode parameter is set to rb which means open the file in binary mode for reading.
A better way is to use with:
with open('file.dat', 'rb') as f:
data = f.read()
print(data)
No need to explicitly close the file.
If you know that the file contains text, possibly encoded in some specific encoding, e.g. UTF8, then you can specify the encoding when you open the file (Python 3):
with open('file.dat', encoding='UTF8') as f:
for line in f:
print(line)
In Python 2 you can use io.open().

Write decoded from base64 string to file

the question is that how to write string decoded from base64 to a file? I use next piece of code:
import base64
input_file = open('Input.txt', 'r')
coded_string = input_file.read()
decoded = base64.b64decode(coded_string)
output_file = open('Output.txt', 'w')
output_file.write(decoded)
output_file.close()
Input.txt contains base64 string (smth. like PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiPz48cmV2aW). After script execution I suppose to see xml in Output.txt but output file contains some wrong symbols (like <?xml version="1.0" encoding="UTF-8"?><review-case create®vFFSТ#2). At the same time if I not read from base64 string from file Input.txt but specify it in script as coded_string = '''PD94bWwgdmVyc2lvbj0iMS4wIiBlbm...''' then Output.txt contains correct xml. Is this something wrong with utf encoding? How to fix this? I use Python2.7 on Windows 7. Thanks in advance.
You probably figured out, now 5 years later, but here is the solution if anyone needs it.
import base64
with open('Input.txt', 'r') as input_file:
coded_string = input_file.read()
decoded = base64.b64decode(coded_string)
with open('Output.txt', 'w', encoding="utf-8") as output_file:
output_file.write(decoded.decode("utf-8"))
under windows you open with 'rb' instead of 'r'.
in your case your code should be :
input_file = open('Input.txt', 'rb')
instead of
input_file = open('Input.txt', 'r')
btw:
http://docs.python.org/2/tutorial/inputoutput.html#reading-and-writing-files
On Windows, 'b' appended to the mode opens the file in binary mode, so there are also modes like 'rb', 'wb', and 'r+b'. Python on Windows makes a distinction between text and binary files; the end-of-line characters in text files are automatically altered slightly when data is read or written. This behind-the-scenes modification to file data is fine for ASCII text files, but it’ll corrupt binary data like that in JPEG or EXE files. Be very careful to use binary mode when reading and writing such files. On Unix, it doesn’t hurt to append a 'b' to the mode, so you can use it platform-independently for all binary files.
hope it helps

Python 3.3 - Need help writing a .png file with data from a .txt

Using Python V.3.3
I was wondering how to create a .PNG (or any other picture file) by using hex data that was written in a notepad document. Currently it reads a picture file. From there it turns it into hex format then saves to a notepad document. It then reads the notepad file and grabs the data.
The problem I am having is that when it tries to write a new picture file it does, but there is no data stored. No matter what I try I end up with a blank, 0 byte picture. How do I fix this? Is there any specific format I need to use on my getbyte variable? Any help would be much appreciated. I'm trying to get this to work to possible send/store data easier for 2D game maps.
import binascii
f = open("c:/test1.png", "rb")
ima = f.read()
f.close()
print (binascii.hexlify(ima))
f = open("file123.txt", "w")
f.write(binascii.hexlify(ima).decode('utf-8'))
f.close()
#-----------
f = open("file123.txt", "r+")
getbyte = f.read()
f.close()
getbytes = (binascii.unhexlify(getbyte))
getbyte = (binascii.hexlify(getbytes))
f = open("filetest.png", "wb")
f.write(getbyte)
f.close
#-----------
To save it as a binary image, write getbytes:
getbytes = (binascii.unhexlify(getbyte))
f = open("filetest.png", "wb")
f.write(getbytes)
f.close
I think you are also looking at the wrong directory, try to save under a different name and see if it creates that file.

Categories