943,685 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 5616
  • Python RSS
Feb 27th, 2009
0

Reading image as binary file

Expand Post »
Hello,

Is there a way to read an imagefile into a binary string?

I know I can use
Python Syntax (Toggle Plain Text)
  1. 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!
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Bart6114 is offline Offline
5 posts
since Feb 2009
Feb 27th, 2009
0

Re: Reading image as binary file

Search this forum for Vega's decimal to binary converter. Your code is reading the data correctly; now you just have to find the right way to display it.

Jeff
Reputation Points: 92
Solved Threads: 156
Practically a Master Poster
jrcagle is offline Offline
608 posts
since Jul 2006
Feb 28th, 2009
0

Re: Reading image as binary file

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 ;).

Python Syntax (Toggle Plain Text)
  1. while n >0:
  2. Bstr=str(n % 2) + Bstr
  3. n= n>>1
  4. print(n)

While this works fine for decimals, it does not read binarystrings that I opened trough;

Python Syntax (Toggle Plain Text)
  1. File=open(FILE, "rb")
  2. for line in File:<blockquote>Bstr=(line % 2) + Bstr</blockquote>

Because this doesn't work for bin types.

for example;
Quote ...
>>> 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
>>>
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!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Bart6114 is offline Offline
5 posts
since Feb 2009
Mar 1st, 2009
0

Re: Reading image as binary file

I took code I had to dump hexadecimal image data and added a binary option to it ,,,
python Syntax (Toggle Plain Text)
  1. # hexadecimal and binary dump of image file data
  2. # modified to work with Python30
  3.  
  4. import binascii
  5.  
  6. try:
  7. # pick an image file you have in the working directory
  8. # or give the full file path ...
  9. image_file = 'py.ico'
  10. fin = open(image_file, "rb")
  11. data = fin.read()
  12. fin.close()
  13. except IOError:
  14. print("Image file %s not found" % imageFile)
  15. raise SystemExit
  16.  
  17. # convert every byte of data to the corresponding 2-digit hexadecimal
  18. hex_str = str(binascii.hexlify(data))
  19. # now create a list of 2-digit hexadecimals
  20. hex_list = []
  21. bin_list = []
  22. for ix in range(2, len(hex_str)-1, 2):
  23. hex = hex_str[ix]+hex_str[ix+1]
  24. hex_list.append(hex)
  25. bin_list.append(bin(int(hex, 16))[2:])
  26.  
  27. #print(bin_list)
  28. bin_str = "".join(bin_list)
  29. print(bin_str)
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.
Last edited by vegaseat; Mar 1st, 2009 at 3:06 am.
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Mar 1st, 2009
0

Re: Reading image as binary file

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!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Bart6114 is offline Offline
5 posts
since Feb 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Sync-up Python, wxPython, XRCed, BoaConstructor
Next Thread in Python Forum Timeline: How do I loop or repeat a program?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC