Mean color in a part of the picture

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
Eric B
Posts: 14
Joined: 2010-07-05T04:27:52-07:00
Authentication code: 8675308

Mean color in a part of the picture

Post by Eric B »

Hello,
my use case seems quite classic, but I did not find direct answer yet. I started to write this thread looking for a solution, but found one in between, so more a way to share and discuss it ;)
I am using IM for a while to resize my jpg for the web. I like to put the title of the picture directly on it. I used to put it on a frame outside the pict, but would like to do it on the top left on the image itself (-gravity NorthWest).
I want to determine the color I should use to write the title.

Basically, I need to identify the mean color of the first 800x50 pixels and get the negative of it.
Actually, I even need to do it in black/white, inverting a middle color (or a middle gray if in colorspace gray) does not help much...

While looking for a solution, I ended up with the following command:

Code: Select all

convert input.jpg -crop 800x50+0+0 -threshold 50% -negate -scale 1x1\! -format '%[pixel:s]' info:-
-> it return 'black' or 'white', which is the color name that I should use to write my title!

Seems to work fine, do you have maybe even better command or additional ideas to do this job?

In my PowerShell script, I use it like that:

Code: Select all

$headlineColor = convert $file -crop 800x50+0+0 -negate -scale 1x1\! -format '%[pixel:s]' info:-
convert -size 1000x50 xc:transparent -font Candice -pointsize 20 -gravity west -draw "fill '$headlineColor' text 0,0 '$headline'" -trim +repage headline.png
#(... generating also a copyright file, calculating the size, generating the output filename, etc)
convert $file -filter Lanczos -resize $resize -unsharp 0x0.6+0.75+0.02 -format JPEG -quality 95 -gravity SouthEast $signatureFile -compose atop -geometry +4+0 -composite -gravity NorthWest headline.png -compose atop -geometry +4+0 -composite $s
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Mean color in a part of the picture

Post by fmw42 »

This should be quicker for a grayscale image

mean in the range of 0 to quantumrange (255 for Q8 and 65535 for Q16 IM compile)

convert input.jpg -crop 800x50+0+0 +repage -negate -format "%[mean]" info:

or

mean in range of 0 to 100

convert input.jpg -crop 800x50+0+0 +repage -negate -format "%[fx:100*mean]" info:

see
http://www.imagemagick.org/script/escape.php
http://www.imagemagick.org/Usage/transform/#fx_escapes


I am not sure why you put the threshold 50% in there, if you want the color value.
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Mean color in a part of the picture

Post by Bonzo »

There have been two similar posts on this subject and I have found one that may be of help: viewtopic.php?f=1&t=18788

The problem finding old posts is the thread title is not always very helpful.
Eric B
Posts: 14
Joined: 2010-07-05T04:27:52-07:00
Authentication code: 8675308

Re: Mean color in a part of the picture

Post by Eric B »

indeed, giving a good subject title is always a tricky part. For instance, I ask about the mean color, but actually are more looking for "black or white labeling" as the post you mentioned, Bonzo.

@ fmw42: I am using threshold exactly to get 'black' or 'white' and not the mean color directly, because I realized that writing in the opposite color maybe useless when it is exactly in the middle: inverting (128,128,128) will give the same, such a gray sky is not uncommon! Another improvement would be to surround the title.

I see however your answer in that other topic with your similar suggestion, finishing by returning "black" or "white" depending if the mean color is > or <50.
Basically, at the end, it is more a performance question: threshold 50% vs mean color + value test. I suppose it depends on the shell as well...
Post Reply