Page 1 of 1

Raw Bytes to Image conversion...

Posted: 2017-02-22T11:43:00-07:00
by frandallfarmer
I posted this to stackoverflow before I discovered ImageMagick...
http://stackoverflow.com/questions/4238 ... age-format

"How do I convert a file with up to 320k of bytes into an image with with 1, 2, or 3 bytes (any will do) of color?
I'm wanting to show off how small my 1980's era application is by representing 100% of the 320k of data as a bitmap image printed onto my project card.
I know the data won't look like anything but noise, but that's OK. I just want to be able to say: "That's it - those pixels uniquely represent every byte in this application!"
It'd be cool if there was a "2D barcode" app that does this, but after an hour of searching I couldn't find one that had the capacity and was implemented (plenty of papers.)"

Can ImageMagick do this for me? Stream of bytes -> 8 bit color image. I'm sure I'll have to give a image size that can hold the data, but what switches/formats can I use with ImageMagick to do this?

Thank you so much. If you're on SO, please give your answer there so I can vote it up and choose it as best answer. :-)

Re: Raw Bytes to Image conversion...

Posted: 2017-02-22T12:26:07-07:00
by fmw42
try

Code: Select all

convert image -threshold XX% image_1byte.gif

Code: Select all

convert image -monochrome image_1byte.gif

Code: Select all

convert image +dither -colors 2 -depth 2 image_2byte.gif

Re: Raw Bytes to Image conversion...

Posted: 2017-02-22T12:45:00-07:00
by snibgo
I think the OP has 320,000 bytes of any old data, and wants to make this into an 8-bit/channel, 3-channel image.

First, decide what image size you want. You need (width * height) = 320000/3. For example, approx 326x326 pixels. This would need a filesize of 318,828 pixels. Suppose the file is called in.bin. Then:

Code: Select all

convert -size 326x326 -depth 8 RGB:in.bin out.png

Re: Raw Bytes to Image conversion...

Posted: 2017-02-22T12:49:51-07:00
by frandallfarmer
Wow! That's a quick reply, but I'm confused by those examples.
Are you assuming the input bytes are coming on STDIN?

Lets say my source file is called input.bin and the image will be output.gif - how would those change?

Thanks so much for the help...

Re: Raw Bytes to Image conversion...

Posted: 2017-02-22T12:50:26-07:00
by frandallfarmer
Ugh. Only saw the code. Sorry.

Re: Raw Bytes to Image conversion...

Posted: 2017-02-22T12:56:24-07:00
by snibgo
Okay, do you understand now? The main difficulty is deciding on the image size. If you have 3*N bytes, the image could be Nx1 or 1xN pixels. If N is a prime number, those are the only possibilities. (But you might pad extra bytes to the file.)

Re: Raw Bytes to Image conversion...

Posted: 2017-02-22T12:58:21-07:00
by frandallfarmer
Getting factors for my file size is easy.
The only problem I'm having now is this:

convert -size 194x92 -depth 8 RGB:caribe1.d64 out.png
convert.im6: unexpected end-of-file `caribe1.d64': No such file or directory @ error/rgb.c/ReadRGBImage/235.

I know the file is there, because bash filled it in for me auto-complete. :-)

Re: Raw Bytes to Image conversion...

Posted: 2017-02-22T12:59:38-07:00
by frandallfarmer
It's 17484 bytes - we're talking 1 byte per pixel, right? (aka 256 color)

Re: Raw Bytes to Image conversion...

Posted: 2017-02-22T13:03:55-07:00
by snibgo
See my first post. 194*92*3 = 53544.

Re: Raw Bytes to Image conversion...

Posted: 2017-02-22T13:17:20-07:00
by frandallfarmer
This is working for me. Thank you very much!

convert -size 194x92 RGB:caribe1.d64 out.tiff

Re: Raw Bytes to Image conversion...

Posted: 2017-02-22T13:38:08-07:00
by frandallfarmer
Image

That is two sides of a Commodore 64 disk image that holds Lucasfilm's Habitat/Club Caribe, the first MMO/Virtual World.

I'm leading an open source effort to bring it back to life @ http://neohabitat.org http://github.com/frandallfarmer/neohabitat

Thanks for the help!