http://qs321.pair.com?node_id=11126479


in reply to Adding a watermark to an image with GD::Image

Prossibly use Imager; it has methods like getcolorcount or getcolorusage where you could maybe look and see how used your watermark color is.

Edit: rough sample finding a single color. You'd need to walk the color histogram hash and maybe look and find how many pixels are using below whatever threshold of "too dark" pixel.

my $img = Imager->new->open( file => q{myfile.jpg} ); my $colors = $img->getcolorusagehash(); my $color_black = pack( q{CCC}, 0, 0, 0 ); say qq{True black used }, $colors->{$color_black}, qq{ times.}

Edit again: More thinking about it what I'd do (vague handwaving, but I offer a link!) is use the histogram hash and sort the keys (they're packed RGB values so when you sort them asciibetically it should do the right thing). You can compute a luminance value you consider "too dark" and then figure out what percentage of the image pixels are less than that value; if it's over whatever threshold, swap to your backup light color. Theoretically you could even copy out the subsection of your image where you're contemplating putting the watermark and just get the color usage for that subsection.

Another thought: Actually looking at your second sample image another option rather than changing the watermark color might be if it's too dark then just write it first 2 pixels larger in a lighter color and then write the dark version on top (so it'd be outlined).

The cake is a lie.
The cake is a lie.
The cake is a lie.