Page 1 of 1

Mean color in a part of the picture

Posted: 2013-06-28T03:02:29-07:00
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

Re: Mean color in a part of the picture

Posted: 2013-06-28T09:00:49-07:00
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.

Re: Mean color in a part of the picture

Posted: 2013-06-28T09:35:42-07:00
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.

Re: Mean color in a part of the picture

Posted: 2013-07-01T03:07:10-07:00
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...