[Image-SIG] Converting a binary file to ascii

Ned Batchelder ned at nedbatchelder.com
Wed Sep 3 13:27:43 CEST 2008


Since you are on Windows, you need to provide the "rb" mode to open, or 
it will convert newline characters in your file, which can trash your 
binary data.  Also, I don't understand what b2a_uu does, but the docs 
say its input string should be at most 45 characters, so it doesn't seem 
like what you want.

When you say "convert it to an equivalent ascii string", what do you mean?

--Ned.
http://nedbatchelder.com

Ashish Sethi wrote:
> I have PPM-P6 image file which is in binary format .The file is of the type  :-
>
> P6
> 128 128
> 255
> ߢ…ä£{ä¡~ߟxÞ¡yàšfß›lÜšyâ›jåŸhæ¦ué­yê§vé¡pÛš»ubž^Y¦f](c)g ..........and so on.
>
> How can i read the 4th line (binary) and convert it to an equivalent
> ascii string. I used the following method but couldn't get the desired
> result..
>
> import binascii
> from binascii import b2a_uu
> infile=open("C:\\Documents and Settings\\Desktop\\lena.ppm")
> a=infile.readline()
> b=infile.readline()
> c=infile.readline()
> d=infile.readline()
> z=b2a_uu(d)
> print z
>
> This script gave me some random absurd data as output.
> Plz help
> _______________________________________________
> Image-SIG maillist  -  Image-SIG at python.org
> http://mail.python.org/mailman/listinfo/image-sig
>   

-- 
Ned Batchelder, http://nedbatchelder.com




More information about the Image-SIG mailing list