How to fx a (pref. non-reversible) watermark 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
User avatar
Marsu42
Posts: 75
Joined: 2014-06-12T03:17:45-07:00
Authentication code: 6789
Location: Berlin

How to fx a (pref. non-reversible) watermark background?

Post by Marsu42 »

I've written a watermarking batch script that puts a semi-transparent plain overlay in the image, brightening or darkening the background according to the image below. It's done by just soft-light'ing a semi-transparent png. The real watermark image/text gets written on top of that.

Problem: I would like to make it look fancier :-) and if possible non-reversible. Right now you can just batch-remove it by doing the reverse process, though some luma/chroma resolution loss would probably occur. Yes, I know watermarks never really protect images, but there's no harm in trying, eh? Now I see there is the rather new fx operator, but I couldn't find any appropriate samples and it's way too complicated for me to come up with something completely on my own.

Question: How do I fx a blur, glass or other distortion effect on the background? The area mask which should be covered is given in a png. My preference would be "convert" instead of "composite" to save a step. It would be nice if a random component would be there vs. batch removal, but this is an optional extra - anything other than simple background brighten/darken would be nice.

Thanks for any help (and sorry if the answer is already out there and I am unable to find it)!
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How to fx a (pref. non-reversible) watermark background?

Post by snibgo »

The possibilities are endless. I'm quite fond of this:

Create a watermark, white on black background:

Code: Select all

convert -size 400x450 -background Black -fill White label:snib -trim wm.png
Apply the watermark to the wizard image:

Code: Select all

convert wizard: ( +clone -blur 0x5 -modulate 90,90,90 ) wm.png -alpha off -gravity center -composite out.png
Where the watermark is white, this blurs, darkens, lowers saturation and shifts hue. A bit like coloured frosted glass. Put anything you like in "-blur 0x5 -modulate 90,90,90".

"-fx" is quite old. It is interpreted for every pixel, so is very slow on large images.
snibgo's IM pages: im.snibgo.com
User avatar
Marsu42
Posts: 75
Joined: 2014-06-12T03:17:45-07:00
Authentication code: 6789
Location: Berlin

Re: How to fx a (pref. non-reversible) watermark background?

Post by Marsu42 »

snibgo wrote:The possibilities are endless.
Yeah, that would be my problem :-p
snibgo wrote:I'm quite fond of this
This is a terrific sample, thanks! As to be expected, I have follow-up questions :-p

1. Bug: In your sample script, the -gravity doesn't do anything over here, the watermark is positioned to the northwest whatever the parameter

2. How do I resize and position (from the border)? With my former good ol' -draw method, the "image dissolve x,y a,b wm.png" did that for me :-o

3. Is there a way to add an overall brightness (other than applying yet another overlay) since your watermark on black background is also black?

4. What's the general method of clamping an effect to a mask? For example if I want to use a script like this, but only apply it to the area covered by the png mask: http://www.fmwconcepts.com/imagemagick/ ... /index.php

Thanks again so much for the help, once I get it working that will be great!
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How to fx a (pref. non-reversible) watermark background?

Post by snibgo »

1. I have no idea why I put "-gravity center" there. It should have no effect as the first two images are exactly the same size.

2. You can adjust the mask, eg by extending it:

Code: Select all

convert wizard: ( +clone -blur 0x5 -modulate 90,90,90 ) ( wm.png -extent 600x600-100-20 ) -alpha off  -composite out.png
3. Doing a darken or lighten, as required, in a single step is tricky. With a different scheme we could "-compose AddModulus -composite" etc.

4. A general method of clamping to a mask is what I did: I made a second image (blurred, darker etc) and composed it over the original, but masked by wm.png, so the effect was present only where the mask was white. Other masking options include "-mask ... +mask" and "compose CopyOpacity".
snibgo's IM pages: im.snibgo.com
User avatar
Marsu42
Posts: 75
Joined: 2014-06-12T03:17:45-07:00
Authentication code: 6789
Location: Berlin

Re: How to fx a (pref. non-reversible) watermark background?

Post by Marsu42 »

snibgo wrote:2. You can adjust the mask, eg by extending it
Thanks, I've got it working - looks great: not "off the shelf", rather unobstrusive but still stands out a bit, plus I imagine it's not reversible that easily w/o major artifacts.
snibgo wrote:3. Doing a darken or lighten, as required, in a single step is tricky. With a different scheme we could "-compose AddModulus -composite" etc.
Um, could you please elaborate on that? Darken is covered as the modulate does it, but for brightening I have absolutely no clue how to exactly use the AddModulus in the command line above. As a IM noob, I'm afraid the online help is not very helpful for these rather complicated arguments :-o
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How to fx a (pref. non-reversible) watermark background?

Post by snibgo »

Anything is reversible, in the sense of doing it by hand in Gimp or whatever. IM's blur could be automatically reversed, in theory, but I don't know how.

The first parameter to "-modulate" is the brightness. See http://www.imagemagick.org/script/comma ... p#modulate . Use "110" etc to lighten. But if the image is white, it won't lighten any further.

With a different scheme we could "-compose ModulusAdd -composite" etc. Eg, Windows BAT syntax:

Code: Select all

convert -size 400x450 -background Black -fill Gray50 label:snib -trim wm2.png

convert ^
  wizard: ^
  ( +clone ^
    wm2.png ^
    -gravity Center -geometry -50+50 ^
    -compose ModulusAdd -composite ^
    +repage ^
    -alpha set -channel A -evaluate set 50%% ^
  ) ^
  -geometry +0+0 ^
  -compose Over -composite ^
  out2.png
(Gravity center does make sense here. This is easily reversible.)

See http://www.imagemagick.org/Usage/ . There doesn't seem to be a page specifically about watermarking, but pretty much anything could be used.
snibgo's IM pages: im.snibgo.com
User avatar
Marsu42
Posts: 75
Joined: 2014-06-12T03:17:45-07:00
Authentication code: 6789
Location: Berlin

Re: How to fx a (pref. non-reversible) watermark background?

Post by Marsu42 »

snibgo wrote:Anything is reversible, in the sense of doing it by hand in Gimp or whatever.
Indeed, but you have to know the algorithm and when data loss occurred the reversed image will have less resolution. I just don't want the standard luma watermarked that can be removed by applying the inverse mask.
snibgo wrote:But if the image is white, it won't lighten any further.
... and if the image is pitch black, multiplying zero with anything is still zero. I've added a detection for the background brightness so I can apply different watermarks with this method: http://www.rubblewebs.co.uk/imagemagick ... example=72
snibgo wrote:With a different scheme we could "-compose ModulusAdd -composite" etc.
Thanks, the advantage of this method is that it saves me hardcoding an alpha in the white
snibgo wrote:See http://www.imagemagick.org/Usage/ . There doesn't seem to be a page specifically about watermarking, but pretty much anything could be used.
Thanks for the link. Ok, actually I did have a glance at the docs before :-) ... but IM wizards probably underestimate that getting to know what-does-what is difficult w/o samples that do roughly the intended operation - and I couldn't find any on this. Many ways to achieve the same thing can be a blessing and flexible, but it can also be confusing at first.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How to fx a (pref. non-reversible) watermark background?

Post by snibgo »

Marsu42 wrote:... but IM wizards probably underestimate that getting to know what-does-what is difficult w/o samples that do roughly the intended operation ...
I agree, and sympathise. That is why I write my web pages, because I often find myself wanting to do something similar to something else I once did, but have totally forgotten how I did it.

I very rarely watermark. If I did, I would put it in a web page.
snibgo's IM pages: im.snibgo.com
User avatar
Marsu42
Posts: 75
Joined: 2014-06-12T03:17:45-07:00
Authentication code: 6789
Location: Berlin

Re: How to fx a (pref. non-reversible) watermark background?

Post by Marsu42 »

snibgo wrote:

Code: Select all

-alpha set -channel A -evaluate set 50%%[/quote]

Ugh, fyi: after running this on some real-life pictures this isn't fit for prime time - it has hard breaks on it and reverses color channels though it should really just work on the luma. The good part is that it works on pure black as well as pure white, other than the build-in -draw operators. Any suggestions how to bugfix this? Thanks!

Here's a sample output: http://bitbucket.org/Marsu42/ml-pull/downloads/out2.jpg
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How to fx a (pref. non-reversible) watermark background?

Post by snibgo »

I love that image! But I can see why you might not. Try lowering the alpha percentage from 50% to 25%, or 10%, or 1%. To limit the effect to tones, eg the Lightness of LAB:

Code: Select all

convert ^
  wizard: ^
  ( +clone ^
    wm2.png ^
    -geometry -50+50 ^
    -colorspace LAB -channel R ^
    -compose ModulusAdd -composite ^
    +channel -colorspace sRGB ^
    +repage ^
    -alpha set -channel A -evaluate set 20%% ^
  ) ^
  -geometry +0+0 ^
  -compose Over -composite ^
  out.png
Off the top of my head, a watermark might be wanted to:

- brand images, eg embedding a photo credit;
- discourage use out of context;
- provide traceability for stolen images.

This has implications on positioning: small and discrete in one corner, or repeated multiple times across the image? If substantial information is removed from the centre of the image, reversing the watermark automatically will be very difficult but it will also be obtrusive.

A neat trick would be to add noise to the image, taking care that the noise is comparable to any noise already present. (When I made black and white photos, dust on the negative would enlarge to white specks on the print. Painting these out was most effective by dabbing with ink, emulating the grain structure.)

The cutest watermarks I see use a combination of darkening and lightening, eg lightening with text and darkening with a shadow of the text.

If you find a sample you like, provide a link and we can try to figure out how it was done.
snibgo's IM pages: im.snibgo.com
User avatar
Marsu42
Posts: 75
Joined: 2014-06-12T03:17:45-07:00
Authentication code: 6789
Location: Berlin

Re: How to fx a (pref. non-reversible) watermark background?

Post by Marsu42 »

snibgo wrote:- provide traceability for stolen images.
I looked into that, but couldn't figure out (yet :-)) how the stegano command of im works - and it doesn't seem to work with convert in the same step anyway, so I can just resort to a dedicated program. I definitely want to add this if I post 100% res images somewhere.
snibgo wrote: - brand images, eg embedding a photo credit;
- discourage use out of context;
Both of these :-) what makes it a real challenge and probably the reason why I couldn't find any samples. I'd like to apply both goals in
in different strengths by writing one method and then tuning the parameters (like leaving different exif information in according to intended use).

Branding my primary intention. A little hue might look nice and interesting like your "glass" sample, but I definitely don't want to turn green grass into magenta as this just looks broken.

Discouragement an additional extra. If it's low res, it's not very usable out of context anyway, but I'm paranoid enough to want to watermark high-res shots I post around the net. Of course it can be removed, but don't underestimate that a lot of photogs aren't computer geeks so a watermark beyond a simple luma mask can pose a big problem to them.

What's most challenging is to make it work for arbitrary natural background contrasts and textures. For brightness, I already added a detection to make it less obtrusive but still visible. But it still doesn't look even enough, too visible on plain surfaces and too invisible on very noisy ones. I just saw that im also returns the "standard deviation", so that's probably what I should use for the wm strength.

I'll be on the lookout for samples, thanks for your help - and I'm sure this thread might be valuable to a lot of people looking for a nice watermarking algorithm.
User avatar
Marsu42
Posts: 75
Joined: 2014-06-12T03:17:45-07:00
Authentication code: 6789
Location: Berlin

Re: How to fx a (pref. non-reversible) watermark background?

Post by Marsu42 »

Marsu42 wrote:I just saw that im also returns the "standard deviation", so that's probably what I should use for the wm strength.
I reckon I need to brush up my statistics knowledge... any hints anyone on how to best measure the "noisiness" of an image to compute the watermark background strength required (blur radius or luma mask)? standard deviation? kurtosis?? skewness???
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to fx a (pref. non-reversible) watermark background?

Post by fmw42 »

standard deviation
Post Reply