...making Linux just a little more fun!
By Jim Dennis, Jason Creighton, Chris G, Karl-Heinz, and... (meet the Gang) ... the Editors of Linux Gazette... and You!
From Adam Engel
Answered By: Ben Okopnik, Jimmy O'Regan
Hey Gang,
Does anyone know of a script or program that can convert a bitmap image to binary code and print the line(s) of ones and zeros to standard output?
[Ben] Adam, if I didn't know you, my "homework detector" sense would be tingling. As it is, I'm still a bit boggled as to why you'd want to do such a thing, but - all the standard tools that I can think of, off the top of my head, do octal and hex. Binary, well... heck, I'd do what I always do when it takes me more than a few seconds to think of an answer to that kind of question: reach for Perl.
perl -wne'printf "%08b ", ord $_ for split //' foobar.bmp
That'll give you a line of space-separated, 8-bit binary numbers representing the ASCII value of each character in the file. Just to play with the idea, converting it back wouldn't be any harder:
perl -wne'print chr eval "0b$_" for split' foobar.binary
[Jimmy] Sounds like pbm (http://netpbm.sourceforge.net/doc/pbm.html), minus the header:
P1 # feep.pbm 24 7 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 0 0 1 1 1 1 0 0 1 1 1 1 0 0 1 1 1 1 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 1 0 0 1 1 1 0 0 0 1 1 1 0 0 0 1 1 1 0 0 0 1 1 1 1 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1 1 1 1 0 0 1 1 1 1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
The netbpm tools are a standard part of Linux distributions everywhere. Image Magick and The Gimp are able to write it too.
[Ben] I just tried converting some text to PBM format (using 'convert' from ImageMagick), and it's not doing anything like the above; in fact, it created a file that's mostly full of nulls - which displays a white page with the word "hello" in the center when looked at with an image viewer.
Looking at a 1k chunk at the top of it, I get the following ("^@" is how 'less' displays a null):
ben at Fenrir:~$ printf "hello"| convert text:- pbm:-|head -c 1k|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^@^@^@^@^@^@^@^@
[Jimmy] Whoops. I meant the 'Plain PBM' format, which you get using the pnmtoplainpbm program.
[Ben] [grin] One more time, with gusto...
I think you mean "pnmtoplainpnm" (I checked the Debian file list as well as doing a Google search - which found nothing but rebuked me with "Did you mean: pnmtoplainpnm"; there's no 'pnmtoplainpbm' in sight.) That, however, doesn't seem to do it either:
ben at Fenrir:~/Pics$ pnmtoplainpnm smile.pnm | head -1k| less P3 15 15 255 191 191 191 191 191 191 191 191 191 191 191 191 191 191 191 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 191 191 191 191 191 191 191 191 191 191 191 191 191 191 191 191 191 191 191 191 191 191 191 191 0 0 0 0 0 0 255 255 0 255 255 0 255 255 0 255 255 0 255 255 0 0 0 0 0 0 0 191 191 191 191 191 191 191 191 191 191 191 191 191 191 191 0 0 0 255 255 0 255 255 0 255 255 0 255 255 0 255 255 0 255 255 0 255 255 0 255 255 0 255 255 0 0 0 0 191 191 191 191 191 191 191 191 191 0 0 0 255 255 0 255 255 0 255 255 0 255 255 0 255 255 0 255 255 0 255 255 0 255 255 0 255 255 0 255 255 0 255 255 0 0 0 0 191 191 191 191 191 191 0 0 0 255 255 0 255 255 0 0 0 0 0 0 0 255 255 0 255 255 0 255 255 0 0 0 0 0 0 0 255 255 0 255 255 0 0 0 0 191 191 191 0 0 0 255 255 0 255 255 0 255 255 0 0 0 0 0 0 0 255 255 0 255 255 0 255 255 0 0 0 0 0 0 0 255 255 0 255 255 0 255 255 0 0 0 0 0 0 0 255 255 0 255 255 0 255 255 0 255 255 0 255 255 0 255 255 0 255 255 0 255 255
According to the NetPBM project page/"pbm" man page, "Plain PBM" is created by the "pnmtoplainpnm" utility - although they do state that it only works with monochrome images.
Ah-HA! I've got it - sorta. "pnmtoplainpnm" takes either a PNM or a PBM file, and applies a /reductio ad absurdum/ algorithm to produce the "simplest" version of the input format is. This does indeed do... something similar to what the manpage described:
ben at Fenrir:~/Pics$ printf "foo"|convert -crop 25x15+40+45 text:- pbm:-|pnmtoplainpnm P1 25 15 0000000000000000000000000 0000000000000000000000000 0000110000000000000000000 0000100000000000000000000 0001110111100011110000000 0000101100110110011000000 0000101000010100001000000 0000101000010100001000000 0000101100110110011000000 0000100111100011110000000 0000000000000000000000000 0000000000000000000000000 0000000000000000000000000 0000000000000000000000000 0000000000000000000000000
I don't know that it actually matches the question that was asked, though. :)
[Raj] Not sure if it might help you, but you can have a look at aalib ASCII-art library http://aa-project.sourceforge.net/aalib . It converts jpegs to ascii.
Or do you want to see the raw binary as it is stored in the bmp file ?
[Ben] Well, sorta. Very sorta. Unless you're a C programmer with time enough to write a converter that uses aa-lib. I've always thought that their demo ('bb') was fantastic, but... it's not something that ever really caught on, and I'm a sort of a Luddite in Linux clothing, so my opinion doesn't count.
What you might be thinking about is "aview/asciiview" - which is what comes closest to "converting" JPG to ASCII; the latter displays JPGs, as well as any other format recognized by the NetPBM kit, in an extremely rough ASCII approximation. However, "aview", which can only read PNM-formatted images, is capable of surprising image quality on a terminal with a tiny font:
xterm -fn 5x7 -geometry 1000x1000 # Enter this command in the new xterm convert logo: pnm:-|aview -driver slang
All good fun, but - still doesn't answer the question as posed. Unless I've totally misread it.
Actually, I just recently downloaded NetPBM for another application for JPEG conversion. As far as the "GUI" tools like Imagemagick and The Gimp go, I being a gui-phobe and graphically challenged to boot, I rarely use them except to demonstrate that there is "another way" to people who have thrown away several hundred dollars a year on Adobe Photoshop. There is no "practical" use for my question. It came out of my recent study of "sed" and the movie "Charlie and the Chocolate Factory" (in which Johnny Depp transforms a giant chocolate bar into a "TV" -sized package with an imaginary contraption). The "real" exorcize,
[Jimmy] Come come. It doesn't sound particularly difficult, let alone demonic.
in Chapter Six of O'Reilly's "Sed & Awk" concerns a bit-mapped file that is represented in binary format. Beyond the scope of the exorcize/sed script, I've been trying to "translate" bitmap images to binary code and vice versa to see if -- I know, I know, this is STUPID, but I never pretended to be a Guru, just a GoTTO -- one can send images as text-files to be "rebuilt" upon receipt so people with low-bandwidth connections wouldn't have to wait forever to download images.
[Jimmy] Erm... I think you have things backwards here. Getting images in a binary format takes less bandwidth than getting the same thing encoded as text.
I chose "binary" merely because that was the code used in the exorcise. My excuse for tardiness handing in my homework is part medical -- had to spend a few days off-line in every sense of the word, and part rational: I finally asked myself, "wouldn't people have thought of this before?
[Jimmy] Yep. uuencode, base64, etc. etc.
If it makes you feel any better, it is possible to use a text representation of binary data with the data: URI (http://www.ietf.org/rfc/rfc2397), which Mozilla and Opera support.
Here's a simple example: data:text/plain;charset=utf8,Ksi%C4%85%C5%BCka%20kucharska That just gives you a simple text page with some Polish text.
Here's a bigger example:
See attached data-test.html
Is that what you were thinking about?
That was it exactly. All that hullaballoo for "Dan and James' going-away present." But thanks. After ten years of sending images back and forth, it just occurred to me that I had no idea what was under the hood. Hence, it made "sense" that plain text would be "lighter" than gifs, jpegs, xml images etc. Thanks again for the explanation and the demo.
[Jimmy] Heh. I already had that example made up: it made sense to the original recipient :)
Hence, it must be doable and I merely can't figure out how to do it, or it's doable but not worth the time/effort and was a silly idea in the first place." On the other hand, if the numbers can be deconstructed in Perl, and reconstructed, why not? They laughed at Willy wonka, and look at him NOW.