0

I have a sample grayscale file z-gray-1x3.png, consisting of a single row of three pixels with grayscale values 0x00, 0x7f, 0xff:

1x3 grayscale image

I tried to write a script using imagemagick, hexdump, sed and tr but got 3-byte pixels instead:

$ magick z-gray-1x3.png -depth 8 RGB:output.txt
$ hexdump -xv output.txt | sed -E 's/^[^ ]+//' | tr -d ' \n'; echo
01017f017f7fffff00ff

(the byte 00 is padding by hexdump to an even number of digits.)

The desired output would be 007f00ff (including padding). Is there a way to modify the imagemagick command to do this?

5
  • I really do not understand what you want. But possibly (guess) use magick z-gray-1x3.png -format "%[hex:u.p{0;0}]%[hex:u.p{1;0}]%[hex:u.p{2;0}]" info: | sed 's/\#//g'. The sed is to remove the # sign from before each hex value. This is not tested, so work with the concept. See the very bottom of imagemagick.org/script/fx.php. See also imagemagick.org/script/escape.php Commented Sep 29, 2024 at 5:35
  • Add -depth 8 to my command above after reading the input image. Commented Sep 29, 2024 at 16:17
  • fmw42: Thank you for your suggestion, but it didn't work. With -depth 8 added, it gave 010101010101010101, not what I wanted. Let me try again: z-gray-1x3.png has 3 pixels. I wish to dump them to a file of 3 hex bytes. I can use the Mac application GraphicConverter to export z-gray-1x3.png to a "raw" format, consisting of just the three hex bytes. I can then do hexdump -C z-gray-1x3.raw whose first output line includes the 3 bytes I want: 00000000 01 7f ff Can Imagemagick export my image to something like GraphicConverter's "raw" format, and how? Commented Sep 29, 2024 at 16:49
  • 1
    If your input is in an image format such as PNG, then it can export as raw. See imagemagick.org/Usage/formats/#rgb Commented Sep 29, 2024 at 19:16
  • Thank you, that's what I was looking for. hexdump -C z-gray-1x3.gray | head -n 1 | tr -d "\147-\377" | sed 's/.\{10\}\(.*\)/\1/' | tr -d ' .' -> 017fff Commented Sep 29, 2024 at 19:51

1 Answer 1

1

I think you want to convert to greyscale, rather than RGB, so that you don't get repeated RGB components, and also output in "plain" hexdump style:

magick 3pixels.png gray:- | xxd -p
017fff

Just in case you use a Q16 version of ImageMagick, add in -depth 8 like this:

magick 3pixels.png -depth 8 gray:- | xxd -p
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.