Watermark color depending on the background

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
AKapaceb
Posts: 8
Joined: 2019-03-21T00:52:55-07:00
Authentication code: 1152

Watermark color depending on the background

Post by AKapaceb »

Hello Friends!

Help me with command...

How can determine the color of a particular selected area in the picture, so that later i can insert a watermark depending on the background color. If the color background is light, then insert a black watermark. Or if the background color is black, then insert the lighter!

Some examples:
Image
Image
Image

Thank you so much! :)
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Watermark color depending on the background

Post by snibgo »

What version of IM, on what platform?

The basic idea is to crop the desired rectangle, and find the average lightness. If this is less than 50% (or whatever threshold you want) the text will be white, otherwise black.

With v7 this can probably be done in one command. In v6 it will need a shell script.
snibgo's IM pages: im.snibgo.com
AKapaceb
Posts: 8
Joined: 2019-03-21T00:52:55-07:00
Authentication code: 1152

Re: Watermark color depending on the background

Post by AKapaceb »

Image
Debian 8
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Watermark color depending on the background

Post by snibgo »

For example, bash syntax:

Code: Select all

PERCENT=$(convert \
  toes.png \
  -gravity East \
  -crop 40x200+0+0 +repage \
  -format "%[fx:mean<0.5?100:0]" \
  info:)
For the fill in the command that annotates or labels the image, use:

Code: Select all

-fill "gray($PERCENT%)"
snibgo's IM pages: im.snibgo.com
Post Reply