Output colormap to text (not histogram)

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
tresorjoe
Posts: 13
Joined: 2018-04-12T02:19:12-07:00
Authentication code: 1152

Output colormap to text (not histogram)

Post by tresorjoe »

Hello,

when using identify -verbose image.png for an indexed image, I get following information:

[...]
Colors: 10
Histogram:
1689805: ( 62, 55, 61) #3E373D srgb(62,55,61)
891326: ( 68, 65, 96) #444160 srgb(68,65,96)
531139: ( 80, 67, 80) #504350 srgb(80,67,80)
502187: ( 94, 80,105) #5E5069 srgb(94,80,105)
293317: ( 98,100,154) #62649A srgb(98,100,154)
448022: (100, 94,130) #645E82 srgb(100,94,130)
307128: (133,114,144) #857290 srgb(133,114,144)
704235: (152,144,175) #9890AF srgb(152,144,175)
238666: (153, 89, 91) #99595B srgb(153,89,91)
875255: (210,206,218) #D2CEDA srgb(210,206,218)
Colormap entries: 11
Colormap:
0: (133,114,144,255) #857290FF srgba(133,114,144,1)
1: ( 80, 67, 80,255) #504350FF srgba(80,67,80,1)
2: (152,144,175,255) #9890AFFF srgba(152,144,175,1)
3: ( 62, 55, 61,255) #3E373DFF srgba(62,55,61,1)
4: (210,206,218,255) #D2CEDAFF srgba(210,206,218,1)
5: ( 68, 65, 96,255) #444160FF srgba(68,65,96,1)
6: ( 94, 80,105,255) #5E5069FF srgba(94,80,105,1)
7: (153, 89, 91,255) #99595BFF srgba(153,89,91,1)
8: (100, 94,130,255) #645E82FF srgba(100,94,130,1)
9: ( 98,100,154,255) #62649AFF srgba(98,100,154,1)
10: (255,255,255,255) #FFFFFFFF white
[...]

So far so good. Now when using convert image.png -unique-colors -depth 16 image.txt, the text file includes the values and color order from the histogram. I would need the Colormap values (and order) instead. How can I do that using the command line tool?

Thanks,
Joe
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Output colormap to text (not histogram)

Post by snibgo »

Instead of writing to "image.txt", set "-verbose" and write to "info:". That gives the same output as "identify -verbose".
snibgo's IM pages: im.snibgo.com
tresorjoe
Posts: 13
Joined: 2018-04-12T02:19:12-07:00
Authentication code: 1152

Re: Output colormap to text (not histogram)

Post by tresorjoe »

I always get the histogram list instead of the colormap list (see above).
How can I extract this section from -verbose and write it into a text file?:

Colormap:
0: (133,114,144,255) #857290FF srgba(133,114,144,1)
1: ( 80, 67, 80,255) #504350FF srgba(80,67,80,1)
2: (152,144,175,255) #9890AFFF srgba(152,144,175,1)
3: ( 62, 55, 61,255) #3E373DFF srgba(62,55,61,1)
4: (210,206,218,255) #D2CEDAFF srgba(210,206,218,1)
5: ( 68, 65, 96,255) #444160FF srgba(68,65,96,1)
6: ( 94, 80,105,255) #5E5069FF srgba(94,80,105,1)
7: (153, 89, 91,255) #99595BFF srgba(153,89,91,1)
8: (100, 94,130,255) #645E82FF srgba(100,94,130,1)
9: ( 98,100,154,255) #62649AFF srgba(98,100,154,1)
10: (255,255,255,255) #FFFFFFFF white
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Output colormap to text (not histogram)

Post by snibgo »

If r.png is a palette file ...

Code: Select all

magick r.png -colors 16 -verbose info:
...then the output includes:

Code: Select all

  Colormap entries: 16
  Colormap:
         0: ( 51, 51, 45,255) #33332DFF srgba(51,51,45,1)
         1: ( 43, 47, 30,255) #2B2F1EFF srgba(43,47,30,1)
         2: ( 98, 57, 47,255) #62392FFF srgba(98,57,47,1)
         3: ( 83, 76, 56,255) #534C38FF srgba(83,76,56,1)
         4: (101, 98, 83,255) #656253FF srgba(101,98,83,1)
         5: ( 58, 67, 56,255) #3A4338FF srgba(58,67,56,1)
         6: (161, 59, 44,255) #A13B2CFF srgba(161,59,44,1)
         7: (209, 57, 45,255) #D1392DFF srgba(209,57,45,1)
         8: (237, 51, 52,255) #ED3334FF srgba(237,51,52,1)
         9: (232, 81, 89,255) #E85159FF srgba(232,81,89,1)
        10: (187, 72, 68,255) #BB4844FF srgba(187,72,68,1)
        11: (111,147, 87,255) #6F9357FF srgba(111,147,87,1)
        12: ( 93,132, 62,255) #5D843EFF srgba(93,132,62,1)
        13: (245,243,239,255) #F5F3EFFF srgba(245,243,239,1)
        14: (166,166,186,255) #A6A6BAFF srgba(166,166,186,1)
        15: (140,133,146,255) #8C8592FF srgba(140,133,146,1)
But IM won't create a colormap (palette) unless it needs to. So if I want a colormap from an ordinary RGB file, I need "-type palette" before the output.

EDIT: I fluffed the formatting, sorry. Now corrected.
snibgo's IM pages: im.snibgo.com
tresorjoe
Posts: 13
Joined: 2018-04-12T02:19:12-07:00
Authentication code: 1152

Re: Output colormap to text (not histogram)

Post by tresorjoe »

Thanks! That almost does what I need.

magick r.png -colors 16 -verbose info:r.txt
...exports a text file including all the image information as obtained with the identify -verbose command.

How can I reduce this information to contain only the section within your "code" example? I probably didn't understand what you meant with magick r.png -colors 16 -verbose info:"code" as typing this I get a text file called "code" and that's certainly not what you meant :) Any hint?
tresorjoe
Posts: 13
Joined: 2018-04-12T02:19:12-07:00
Authentication code: 1152

Re: Output colormap to text (not histogram)

Post by tresorjoe »

Resolved myself using the grep command:

Code: Select all

magick -verbose image.png info:- | grep -A 10 -i colormap: > image.txt
This just extracts the 10 lines after the colormap entry in the info sequence.
Unless anyone has a more elegant solution that dynamically extracts the colormap information only indpendently from the colors in the palette file?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Output colormap to text (not histogram)

Post by snibgo »

This isn't elegant, but here is a simple Windows BAT script that reads a colormap from an image, and makes a Nx1 image (a clut) of the colours.

I expect the job could be done more simply in bash.

Code: Select all

rem Given %1 is an indexed image,
rem extracts the colormap, making a clut Nx1 image %2.

echo off

setlocal enabledelayedexpansion

set INFILE=%1
set OUTFILE=%2

if "%OUTFILE%"=="" set OUTFILE=gcb.png

set TMP_LIS=\temp\gcb.lis

echo # ImageMagick pixel enumeration: 256,1,255,srgb >%TMP_LIS%

set HASMAP=0
set INMAP=0

for /F "usebackq tokens=1-2 delims=:" %%A in (`%IM%identify ^
  -verbose %INFILE%`) do (

  set FIRST=%%A

  if !INMAP!==1 (
    if "!FIRST:~0,4!"=="    " (
      echo %%A,0: %%B >>%TMP_LIS%
    ) else (
      set INMAP=0
    )
  )
  if !INMAP!==0 if "%%A"=="  Colormap" (
    set HASMAP=1
    set INMAP=1
  )
)

if %HASMAP%==0 (
  echo Input [%INFILE%] has no colormap.
  exit /B 1
) else (
  %IM%convert %TMP_LIS% %OUTFILE%
)
snibgo's IM pages: im.snibgo.com
Post Reply