26

Does there exist a program for Linux that can display raw binary data?

Each byte in my binary files represent a pixel, so it would be very useful if something like this exists where I could say

program_name --input=dat001.bin --width=200 --height=100

and it would display the pixels.

I wonder if gnuplot, can be used for this...?

4
  • 2
    Question does not make sense. To display raw binary data, it must be converted to a human readable form. Usually this form is hexadecimal, which you can use hd to format. Then you ask to see an image. If it is an image, then it is going to be in an image format ( like bmp ) that will at the very least, have a header that identifies the format, the width, height, color depth, and maybe also have a color palette attached.
    – psusi
    Jun 8, 2011 at 1:12
  • 2
    @psusi While the title would be better How to display raw pixel data, the intent of this question is clear, and totally makes sense to me. ("I want a program to let me specify the image meta data") Does it make more sense in hindsight (or perhaps after editing) to you also? Sep 1, 2015 at 12:54
  • @NathanKidd, ahh, yes.. if you know it is an image using a specific format but has no header, rather than being just arbitrary binary data... got ya.
    – psusi
    Sep 4, 2015 at 17:56
  • 1
    ffmpeg can to it: stackoverflow.com/a/15037011/5447906
    – anton_rh
    Feb 9, 2017 at 4:31

5 Answers 5

16

convert from ImageMagick

E.g., an 8-bit 2x3 grayscale:

printf '\x00\xFF\x88\xFF\x00\xFF' > in.bin

Then:

convert -depth 8 -size 3x2+0 gray:in.bin out.png

Command explanation:

  • -depth 8: each color has 8 bits
  • -size 2x3+0: 2x3 image. +0 means starting at offset 0 in the file. If there are metadata headers, you can skip them with the offset.
  • gray:in.bin: the input file is in.bin, and the format is gray, as defined at http://www.imagemagick.org/script/formats.php This weird notation is used because ImageMagick usually determines the format from the extension, but here there is no extension.

How to view such tiny outputs like this example

The problem now is how to view the such a tiny 3x2 output accurately. A direct eog:

eog out.png

is not very good because the image is too small, and if you zoom in a lot eog uses a display algorithm that mixes up pixels continuously, which is better for most pictures, but not for such a tiny image.

One good possibility is to run:

convert out.png -scale 300x out2.png

or directly in one go with:

convert -depth 8 -size 3x2+0 gray:in.bin -scale 300x out.png

-scale is needed instead of -resize, since -resize mixes pixel colors continuously up much like eog by default.

Output:

enter image description here

Anther option is to view it in Gimp:

gimp out.png

Image editors such as Gimp must show every single pixel separately if you zoom in enough.

RGB example

printf '\xFF\x00\x00\x00\xFF\x00\x00\x00\xFF' > in.bin
convert -depth 8 -size 3x1+0 rgb:in.bin out.png

or with the -scale to make it more viewable:

convert -depth 8 -size 3x1+0 rgb:in.bin -scale 300x out.png

enter image description here

Related:

Tested on Ubuntu 16.04, ImageMagick 6.8.9.

3
  • 1
    Another example: if you have a file in RGBA format (say /tmp/tex1.dat), you can view the converted image directly with Imagemagick's display as 128x256 pixel image with display -depth 8 -size 128x256+0 RGBA:/tmp/tex1.dat
    – sdaau
    Jan 15, 2016 at 13:48
  • What if the pixels have 10 ot 12 bits ? I have tried the -endian option but it seems that it works as the -depth 16, the image is very dark
    – sop
    Feb 15, 2017 at 14:28
  • @sop not sure about 10 12. 12 might work with depth 12? 10 I expect you to need to specify further how many bits per channel, since 10 is not divisible by 4 (RGBA) nor 3 (RGB). Not sure how to do this. Feb 16, 2017 at 14:09
16

To see the "raw binary data", I would use the hex dump command hexdump†. I would use the -C option so that I could more easily see telltale ASCII text such as JFIF or PNG in case the data was not raw but a more structured form.

$ hexdump -C example.img
00000000  ff d8 ff e0 00 10 4a 46  49 46 00 01 01 01 00 48  |......JFIF.....H|
00000010  00 48 00 00 ff db 00 43  00 06 04 05 06 05 04 06  |.H.....C........|
00000020  06 05 06 07 07 06 08 0a  10 0a 0a 09 09 0a 14 0e  |................|
00000030  0f 0c 10 17 14 18 18 17  14 16 16 1a 1d 25 1f 1a  |.............%..|
00000040  1b 23 1c 16 16 20 2c 20  23 26 27 29 2a 29 19 1f  |.#... , #&')*)..|
00000050  2d 30 2d 28 30 25 28 29  28 ff db 00 43 01 07 07  |-0-(0%()(...C...|
00000060  07 0a 08 0a 13 0a 0a 13  28 1a 16 1a 28 28 28 28  |........(...((((|
00000070  28 28 28 28 28 28 28 28  28 28 28 28 28 28 28 28  |((((((((((((((((|
*
00000090  28 28 28 28 28 28 28 28  28 28 28 28 28 28 ff c0  |((((((((((((((..|
000000a0  00 11 08 00 80 00 64 03  01 22 00 02 11 01 03 11  |......d.."......|
000000b0  01 ff c4 00 1d 00 00 01  04 03 01 01 00 00 00 00  |................|

I don't know of any image format that consists of unstructured bytes - is the data 8-bit RGB values? If the file contains 30000 bytes is that RGB for 100x100 pixels or RGB for 50x200 pixels or RGB for 200x50 pixels or something else? Is there a palette? You have to know something about the organisation of the data!

To view it as an image I would use the NetPBM utilities or maybe ImageMagick to convert it to a form understood by an image viewer

If the above can't do the job I'd investigate writing a small Perl script


Footnote:

† On some older Unix systems this command might be hd or od with no hexdump command available. See unix.stackexchange.com. od is POSIX and od -Ax -tx1z filename works in some popular Linux distributions.

4
  • Where can I get this? I just did yum search hd on my Fedora desktop, and it didn't have it.
    – Sandra
    Jun 7, 2011 at 23:36
  • OKay. I need to see the pxiels as graphic...
    – Sandra
    Jun 7, 2011 at 23:37
  • 2
    The binary might be called hexdump. On SuSE it is in the util-linux package.
    – Turbo J
    Jun 8, 2011 at 0:30
  • FWIW it's also hexdump in Arch.
    – Sparhawk
    Oct 11, 2014 at 12:20
13

Okay, gnuplot can do it.

http://gnuplot.sourceforge.net/demo_4.4/image.html

6

If not wanting to view it directly but rather convert it the convert utility can do this:

To read from stdin, assuming 320 x 200 pixels, 8 bit gray, header of 0, saving to pic.png in PNG format.

convert -depth 8 -size 320x200+0 gray:- pic.png
1
0

Raw binary data can be easily converted to binary pgm format by adding the header:

echo "P5
200 100
255
"$(cat dat001.bin) > dat001.pgm

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .