'Bulk extract dominant colors from multiple images with imagemagick
I've a number of images that I want to extract the most dominant colors from using Imagemagick. Using this script:
area=$(magick *.png -format "%[fx:w*h]" info:)
magick *.png -kmeans 10 -format "%c" histogram:info: | sed 's/://g' | awk -v area=$area '{print 100*$1/area, "%,", $3}' | sed 's/ *//g' | sort -nr -k1,1 -t ","
I can get a lot of information about the colors in the .pngs in my folder, but I can't see which of the images the information relates to!
I have also looked at this script:
convert *.png -gravity center -crop 1x1+0+0 -format "%f,%[fx:int(mean.r*255)],%[fx:int(mean.g*255)],%[fx:int(mean.b*255)]\n" info:
Which does a great job of giving me info on the pixel in the center of each image, but I can't see what I'm missing to get the first script to output the the original file name and the dominant colors in that file at all? I'm really stumped now, and I'm sure it's something super obvious!
Solution 1:[1]
I think you will have to write a script loop over your images rather than using wild cards in the input. Imagemagick will only report on the first image in the list. If you use a script loop, then you can do something like convert image.suffix -set filename:f "%t_%[mean]" "%[filename:f].suffix". See imagemagick.org/script/escape.php
For example:
Input:
convert lena.png \
-set filename:f "%t_%[fx:round(255*mean.r)]_%[fx:round(255*mean.g)]_%[fx:round(255*mean.g)]" \
"%[filename:f].png"
Result (lena_180_99_99.png):
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | fmw42 |


