Reading image as binary file

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Feb 2009
Posts: 5
Reputation: Bart6114 is an unknown quantity at this point 
Solved Threads: 0
Bart6114 Bart6114 is offline Offline
Newbie Poster

Reading image as binary file

 
0
  #1
Feb 27th, 2009
Hello,

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

I know I can use
  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!
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 608
Reputation: jrcagle is on a distinguished road 
Solved Threads: 150
jrcagle jrcagle is offline Offline
Practically a Master Poster

Re: Reading image as binary file

 
0
  #2
Feb 27th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 5
Reputation: Bart6114 is an unknown quantity at this point 
Solved Threads: 0
Bart6114 Bart6114 is offline Offline
Newbie Poster

Re: Reading image as binary file

 
0
  #3
Feb 28th, 2009
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 ;).

  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;

  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;
>>> 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!
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,025
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 932
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: Reading image as binary file

 
0
  #4
Mar 1st, 2009
I took code I had to dump hexadecimal image data and added a binary option to it ,,,
  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.
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 5
Reputation: Bart6114 is an unknown quantity at this point 
Solved Threads: 0
Bart6114 Bart6114 is offline Offline
Newbie Poster

Re: Reading image as binary file

 
0
  #5
Mar 1st, 2009
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!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC