| | |
Reading image as binary file
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Feb 2009
Posts: 5
Reputation:
Solved Threads: 0
Hello,
Is there a way to read an imagefile into a binary string?
I know I can use
but this produces something unreadable that looks a bit like hexdata (I'm a newbie here :-). What I am trying to accomplish is to convert it to a string of ones and zeros. Is this possible?
Thx in advance!
Is there a way to read an imagefile into a binary string?
I know I can use
Python Syntax (Toggle Plain Text)
open(FILE,"rb")
but this produces something unreadable that looks a bit like hexdata (I'm a newbie here :-). What I am trying to accomplish is to convert it to a string of ones and zeros. Is this possible?
Thx in advance!
•
•
Join Date: Feb 2009
Posts: 5
Reputation:
Solved Threads: 0
Thank you for you're reply, but I still can't figure out how to do this.
Vesa's codesnippet is meant for decimal types and not for binarystrings (atleast to my understanding ;).
While this works fine for decimals, it does not read binarystrings that I opened trough;
Because this doesn't work for bin types.
for example;
And this seems pretty logical as it is not an int, float, or decimaltype. How do you suggest I'd use vesa's sample to output a file opened as binary ("rd") to one's and zero's?
Thx in advance!
Vesa's codesnippet is meant for decimal types and not for binarystrings (atleast to my understanding ;).
Python Syntax (Toggle Plain Text)
while n >0: Bstr=str(n % 2) + Bstr n= n>>1 print(n)
While this works fine for decimals, it does not read binarystrings that I opened trough;
Python Syntax (Toggle Plain Text)
File=open(FILE, "rb") for line in File:<blockquote>Bstr=(line % 2) + Bstr</blockquote>
Because this doesn't work for bin types.
for example;
•
•
•
•
>>> b=bin(5)
>>> b
'0b101'
>>> (b % 2)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: not all arguments converted during string formatting
>>>
Thx in advance!
I took code I had to dump hexadecimal image data and added a binary option to it ,,,
The binary string output is pretty useless, since you can never recover any data back.
If you want to send binary image data as a string, you have to use Python module base64.
python Syntax (Toggle Plain Text)
# hexadecimal and binary dump of image file data # modified to work with Python30 import binascii try: # pick an image file you have in the working directory # or give the full file path ... image_file = 'py.ico' fin = open(image_file, "rb") data = fin.read() fin.close() except IOError: print("Image file %s not found" % imageFile) raise SystemExit # convert every byte of data to the corresponding 2-digit hexadecimal hex_str = str(binascii.hexlify(data)) # now create a list of 2-digit hexadecimals hex_list = [] bin_list = [] for ix in range(2, len(hex_str)-1, 2): hex = hex_str[ix]+hex_str[ix+1] hex_list.append(hex) bin_list.append(bin(int(hex, 16))[2:]) #print(bin_list) bin_str = "".join(bin_list) print(bin_str)
If you want to send binary image data as a string, you have to use Python module base64.
Last edited by vegaseat; Mar 1st, 2009 at 3:06 am.
May 'the Google' be with you!
•
•
Join Date: Feb 2009
Posts: 5
Reputation:
Solved Threads: 0
Thank a lot Vegaseat!
This is exactly what I needed. I know I can't recover any data back. But I'm gonna use it to feed it through a neural net. Seeing the output I think it's not very effective analysing image data (I'm now using PIL for that). But I think it can be very useful while analysing textinput with a neuralnet!
Thanks again!
This is exactly what I needed. I know I can't recover any data back. But I'm gonna use it to feed it through a neural net. Seeing the output I think it's not very effective analysing image data (I'm now using PIL for that). But I think it can be very useful while analysing textinput with a neuralnet!
Thanks again!
![]() |
Similar Threads
- Reading Binary Files in VB (Visual Basic 4 / 5 / 6)
- Image read and write operations in turbo C (C)
- read binary file into 2-Dimensional Array (VB.NET)
- Open In New Window Php (PHP)
- Storing Text file in Database through VB (Visual Basic 4 / 5 / 6)
- outputing image files in c++... (C++)
- Signal Processing: Hadamard Matrices (C)
- Reading a Binary File to a C++ Class (C++)
- reading binary image using C/C++ (C++)
Other Threads in the Python Forum
- Previous Thread: Sync-up Python, wxPython, XRCed, BoaConstructor
- Next Thread: How do I loop or repeat a program?
| Thread Tools | Search this Thread |
address aliased anydbm bash beginner bits calling casino changecolor class clear conversion convert corners count cturtle cursor curves definedlines dictionary digital dynamic dynamically events examples excel external file float format frange function gui handling hints homework i/o iframe import info input java line linux list lists loan loop matching mouse multiple number numbers output parsing path port prime programming projects py py2exe pygame python random rational raw_input recursion recursive scrolledtext searchingfile shebang signal singleton string strings subprocess table tails terminal text thread threading time tkinter tlapse tooltip tuple tutorial type ubuntu unicode urllib urllib2 valueerror variable web-scrape whileloop word wxpython






