Reputation: 655
I want to apply kmeans to an image with an alpha mask. It should only work on the colors that are visible. I'm hoping to one-line it. Starting image:
Naively apply kmeans (whoops, all one color):
convert SpaceshipTransparency.png -kmeans 12x20 KmeansOnly.png
Experiment. Run through all alpha choices. -alpha remove
seems to work somehow but it has a white background? Need to remove this and I found a three step process on ImageMagick docs by outputting a mask as a middle step:
convert SpaceshipTransparency.png -alpha Remove -kmeans 12x20 AlphaRemove.png
convert SpaceshipTransparency.png -alpha extract mask.png
convert AlphaRemove.png mask.png -alpha Off -compose CopyOpacity -composite ThreeStepProcess.png
Upvotes: 0
Views: 114
Reputation: 53174
You can combine commands by chaining using parenthesis processing and in-memory (MPR:) images in Imagemagick.
Try:
convert SpaceshipTransparency.png -write mpr:img +delete \
\( mpr:img -alpha Remove -kmeans 12x20 \) \
\( mpr:img -alpha extract \) \
-alpha Off -compose CopyOpacity -composite OneStepProcess.png
Note: -kmeans is only available on Imagemagick 7. So I recommend using magick rather than convert.
Upvotes: 1