determine "main" color of image border

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
Gozer404

determine "main" color of image border

Post by Gozer404 »

I have a bunch of images used as wallpapers. Some can't be streched or extended to the size of the screen and can only be resized to have the height equal to the height of the screen (mainly "portrait" or vertical mode images).

These images are given to a wallpaper program that can add a border around the images that are not covering the full size of the screen (gnome-background on linux to be more precise).

The question is: how can I with IM find a color which matches the best the actual borders of the vertical images:
- imagine you have a drawing done on a solid background, I want to find the value of this background color;
- now the image is on a sort of gradient background (for example lots of different dark blue colors), I want to find the "most used" color from the borders of the image (at the end when telling the wallpaper program to add a border I will use this found color, so the overall wallpaper looks pretty)

I hope my question is "clear enough" (as my english because that's not my natural language :->)
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Post by anthony »

I think the best way would be to -crop off the four borders, append them into one long image of pixels, the color average them.

Hmmm

Code: Select all

  convert image.png    \( -clone 0 -crop 1x0+0+0 \)  \
           \( -clone 0 -crop 0x1+0+0 -rotate 90 \) \
      -gravity SouthEast \( -clone 0 -crop 1x0+0+0 \)  \
          \( -clone 0 -crop 0x1+0+0 -rotate 90 \) \
      -delete 0 +repage -append -filter Box -resize 1x1\!   -depth 8 txt:-
The Box filter should replace pixels with their average color.

for the builtin "rose:" image I get the color #706658
for logo: I get the offwhite #FEFEFE

Which is probably a bug as it should have been 'white' ! But is is very close!

You can limit it to just the sides, or even get a different color for each side!

Alturnativally you can stretch the edge and blur it more and more as it moves further
for the original image to give a sort of 'continuation effect'. This is especially usful for vetrtical border extending.

The -motion-blur operator would probably be useful for generating this 'extended border'
type of effect. :-)
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Gozer404

Re: determine "main" color of image border

Post by Gozer404 »

Finally I put a reply on my old post: I used this which is analyzing the histogram output, based on your reply:

Code: Select all

convert $file \( -clone 0 -crop 1x0+0+0 \) -gravity SouthEast \( -clone 0 -crop 1x0+0+0 \) -delete 0 +repage -append  histogram:- | sed -n '/^Comment={/,/^}/{ s///; /^$/q; s/^ *//; p; }' | sort -n | tail -1 | awk '{print $NF}'
For the builtin images it gives:
rose: rgb(105,96,81) = #696051
logo: white = #FFFFFF
Post Reply