Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: Adding a watermark to an image with GD::Image

by Fletch (Chancellor)
on Jan 06, 2021 at 22:24 UTC ( #11126479=note: print w/replies, xml ) Need Help??


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.

Replies are listed 'Best First'.
Re^2: Adding a watermark to an image with GD::Image
by Bod (Chaplain) on Jan 06, 2021 at 23:13 UTC
    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).

    WoW! That's some selection of possible solutions from you Fletch. Thank you, it is certainly giving me some ideas to try out.

    The idea of an outline by printing the text twice in different colours certainly has plenty of merit. Potentially it would eliminate the need to sample the background at all. If there's a dark outer part to the text and a lighter inner then one or other should contrast with the background sufficiently.

    I may have to find two suitable TTF sources or find one and make a copy with slightly narrower font strokes so one sits inside the other. That shouldn't be a big problem.

        It's a nice idea, but won't that suffer the same problem? ie. Bod will still need to contrast that outline with the image background otherwise it might still be black-on-black or whatever.


        🦛

Re^2: Adding a watermark to an image with GD::Image
by Bod (Chaplain) on Jan 06, 2021 at 22:42 UTC
    Prossibly use Image

    Nice thought and it looks like a very useful module...

    But it's not an option as installation requires access to the compiler which I don't have on the shared hosting. So I have to use what's there or pure Perl modules.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://11126479]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (3)
As of 2021-04-16 23:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?