Annotate each pixel with the color index

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
LiloLilo

Annotate each pixel with the color index

Post by LiloLilo »

Hi all,

I am trying to enlarge a region of an RGB8 image using scale, split channels and then label each enlarged pixel of each channel with the pixel color index. For the first operation, for example I am using this:

convert image.png -crop 10x10+0+0 -separate -scale 2000% image%d.png

Now, the problem is labelling. As every channel would be 8 bit size, each label would be a number between 0 and 255. It can be done?

Thank you all!
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Annotate each pixel with the color index

Post by fmw42 »

User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Annotate each pixel with the color index

Post by anthony »

Internally IM does not use our understand the use of color indexes. This is something that is specific to very specific image file formats.

As a consequence you can not 'lookup' the index for a specific color within IM.

HOWEVER: When their is a will there is a way.

Some formats like XPM assignes 'characters' (one or two typically) to each color and generates an ascii array of charactered to represent the image. It is quite conceivable that a script can parse this ascii image and re-generate the image using the colors also defined in the header of that image file format. I've seen it done though it was a long time ago.

They will not be color index numbers, but the script can change that too.

And PLEASE whatever you decide, or come up with, please report back or contribute the program/script back to the IM community.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
LiloLilo

Re: Annotate each pixel with the color index

Post by LiloLilo »

Hi all,

here it is the solution I found :-) [copy and paste the following in a .bat file to test]

USAGE: [copy and paste the following in a .bat file to test, suppose TEST.BAT]

TEST Filename Channel CropSize CropLeft CropTop [enter]

Filename: the RGB24 filename you want to process
Channel: R/G/B
CropSize: the size of the square region you want to process
CropLeft: the distance of the region from the left side
CropTop: the distance of the region from the top side

PS. This process generate CropSizexCropSize temporary .bmp files that will be deleted at the end of the process. The result will be stored in OUT.png

convert %1 -channel %2 -separate -crop %3x%3+%4+%5 -scale 2000%% TmpChannel.bmp

convert TmpChannel.bmp -crop 20x20 +repage -gravity Center -fill red -pointsize 10 -annotate 0 %%[fx:%2*255] TmpImage%%02d.bmp

montage -mode concatenate -tile %3x TmpImage*.bmp OUT.png

del TmpImage*.bmp

del TmpChannel.bmp

Any improvement will be wellcome :-)

Thank you all for help, bye!
LiloLilo

Re: Annotate each pixel with the color index

Post by LiloLilo »

just a note: if you are working with a large CropSize, the montage will fail because of the TmpImage*.bmp file ordering by command prompt. To fix this issue, you have to change the second line as follows:

convert TmpChannel.bmp -crop 20x20 +repage -gravity Center -fill red -pointsize 10 -annotate 0 %%[fx:%2*255] TmpImage%%04d.bmp

(I changed TmpImage%%02d.bmp to TmpImage%%04d.bmp)

Here it is a sample:

Image

bye!
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Annotate each pixel with the color index

Post by anthony »

Please note that you are not labeling the images with an index, as was reqested, but the grayscale value of a grayscale image. Also be warned that the given solution is specific to Window Scripting, just as most IM Example solutions are more UNIX specific.

However for gray scale this is a very good solution! And I commend it.

As another method that is suitable for grayscale look at...
Dithering using a list of images....
http://imagemagick.org/Usage/quantize/#diy_symbols

Yes it is an FX solution and slow, and may need some carful checks to select the right image for the right grayscale value, but it should let you do a lot more than just labeling the image.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply