colorsimagemagickpixelgimp

Imagemagick results of +transparent (remove all colors but one) produces color residues


I have images generated. It's pixel-perfect images (no interpolation, 1 pixel=1color, 5 colors only, etc.) I want to separate each color in different files, so I use ImageMagick CLI commands to obtain the unique-colors, then extract with the +transparent command.

But the results contains colors residues.


I actually use this process to separate each color from an image pixel-perfect generated (in other words, 1 pixel, 1 color, no interpolation, no close color, etc. for a total of exactly 5 colors). First I extract all unique-colors :

convert <image.png> -unique-colors txt: > colors.txt

I got a list of the 5 colors as expected. Then I use the following command to separate each color in a unique file with a loop script :

# #110F12 is one of the colors that could be found in the colors.txt
convert <image.png> +transparent "#110F12" <output.png>

When I open the output.png with sxiv (a Simple X Image Viewer) I see some color residues. But I can't see them if I open it with Gimp and display the image at 100% or more, so I was like it was nothing. Because I also need to fill the extracted color to a plain black for a later use, I first did it with Gimp as following :

Because I want to do the same inside a script, to not open Gimp at all, I modified the previous ImageMagick command to :

convert <image.png> +transparent "#110F12" -fill black -opaque "#110F12" <output.png>

But then, if I open both files ImageMagick+Gimp Process & ImageMagick Only with sxiv, the first one (Gimp) has no more color residues (even at =<50% display), the second one have some white outlines.

Here some screenshot of the result on sxiv (the background of sxiv is gray to see clearly see the residues, but in reality it's transparency). First one with additional Gimp process, second one with ImageMagick only. Even if the image processing during the downloading show blurry images, the Gimp one has sharp edges/pixels, no color residues. However, the ImageMagick only one...

Result of ImageMagick then Gimp Result of ImageMagick only

So, I conclude that the residues are real. But I don't know how to get the same result as I did with Gimp, but with ImageMagick only.

Here is a sample for testing. The image contains exactly 5 colors (It's pixel perfect generated), and one of them is #110F12. But the image downloader probably downgrade the quality so it will be blurred if you test it.

sample of five colors


Solution

  • With ImageMagick v7, a command like this will separate an image into individual images, each with just one color from the input image on a transparent background...

    Windows Syntax:

    magick image.png -colors 5 ( +clone -unique-colors ) ^
       +swap -duplicate %[fx:u.w-1] -reverse ^
       +transparent %[pixel:u[-1].p{t,0}] +delete result%02d.png
    

    That reads in the image, and in this case starts by reducing the total colors to 5. Inside parentheses it makes a clone of the image, then reduces that to a single pixel for each individual color with "-unique-colors". That will be used in the next part of the command as a reference image.

    After the parentheses the input image is duplicated to make one for each color. Then the "+transparent" operation steps through all the images, and removes all the colors except one using that "-unique-colors" image as a reference.

    It finishes by deleting that reference image, and outputting the 5 files named "result00.png, result01.png... etc."

    EDITED TO ADD: You can start with the above command, and expand it a bit to write the 5 images and 5 matching images in just black with a Windows CLI command like this...

    magick image.png -strip -colors 5 ( +clone -unique-colors ) ^
       +swap -duplicate %[fx:u.w-1] -reverse -background none ^
       +transparent %[pixel:u[-1].p{t,0}] +delete ^
       -background black -alpha background -write img_color-%02d.png ^
       -fill black -colorize 100 img_black-%02d.png
    

    That will read the input image, separate it into 5 images, each with a unique color on a transparent background, and write those 5 image files. It continues the command by colorizing the 5 images to black, then writing those to files also.

    The filename numbers will coincide, so the all black companion image to "img_color-00.png" will be "img_black-00.png", and so forth.