Watermark removal, please help [solved]

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?".
BradLee
Posts: 23
Joined: 2017-04-11T05:09:01-07:00
Authentication code: 1151

Watermark removal, please help [solved]

Post by BradLee »

Hi,

I have a number of B+W photos that I need to remove a watermark from.
Unfortunately I lost the backup of the original images and only have the watermarked copies.

I am able to do this by layering the watermarked photos with the watermark in paint.net and performing a "difference."
However I have nearly 1200 images to do this for, and it would take me an eternity to do it by hand. :(

So I was hoping that I can achieve something similar in ImageMagick, running it as a batch?

E.g

Watermarked Image:
http://i1219.photobucket.com/albums/dd4 ... fp7j2k.png

Watermark:
http://i1219.photobucket.com/albums/dd4 ... dm6zdg.png

Difference Image:
http://i1219.photobucket.com/albums/dd4 ... gdp604.png

Inverted (final) Image:
http://i1219.photobucket.com/albums/dd4 ... opqyng.png

Can someone please help me with this? I'm really out of my depth. :(

I currently have ImageMagick-7.0.5-4.x86_64.rpm, and am using Windows 7 Pro.

Thanks,
BradLee
Last edited by BradLee on 2017-04-11T19:27:19-07:00, edited 1 time in total.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Watermark removal, please help.

Post by snibgo »

If the watermark contains fully opaque pixels, these will have overwritten the input, so we can't calculate them. A hole-filling ("inpainting") technique might guess them.

If your watermark contains no fully-opaque pixels, and was effectively applied with "-compose Over -composite", and you know exactly where is was applied, the inverse process can easily be applied get the source image back.

Can you post the watermark image, and a watermarked image (or at least the portion that contains the watermark). You can upload to somewhere like dropbox.com and paste the URLs here.

EDIT: While I was replying, you uploaded them. I'll take a look.
snibgo's IM pages: im.snibgo.com
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Watermark removal, please help.

Post by snibgo »

Your watermark is fully opaque, so it clearly wasn't applied with "-compose Over -composite". Do you know how it was applied?
snibgo's IM pages: im.snibgo.com
User avatar
GeeMack
Posts: 718
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: Watermark removal, please help.

Post by GeeMack »

BradLee wrote: 2017-04-11T06:11:21-07:00I am able to do this by layering the watermarked photos with the watermark in paint.net and performing a "difference."
I'm using IM 7.0.5-4 on Windows 10 64. I downloaded your example image with the watermark, and your example watermark image, then I ran this command...

Code: Select all

magick image-with-watermark.png watermark.png -compose difference -composite -negate output.png
The result appears to be the original image with the watermark removed.
BradLee
Posts: 23
Joined: 2017-04-11T05:09:01-07:00
Authentication code: 1151

Re: Watermark removal, please help.

Post by BradLee »

GeeMack wrote: 2017-04-11T09:17:00-07:00

Code: Select all

magick image-with-watermark.png watermark.png -compose difference -composite -negate output.png
The result appears to be the original image with the watermark removed.
Yes, that works. Thanks. :)

Now is there any way I can perform this on a whole folder of images?
Cheers.

EDIT: I think i should be using:

Code: Select all

magick *.png watermark.png -compose difference -composite -negate
But not sure how to output to multiple files?

EDIT2: It would be even better if I could save the output over the top of each of the files, so that they kept the same file names.
Last edited by BradLee on 2017-04-11T09:51:04-07:00, edited 1 time in total.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Watermark removal, please help.

Post by fmw42 »

Try mogrify (IM 6) and magick mogrify (IM 7) with -draw to do the composite. See http://www.imagemagick.org/Usage/basics ... fy_compose. Use difference rather than dst_in.

Mogrify will process all images in a folder. I suggest you create a new folder to hold your output before running mogrify, but it will allow you to keep the same names.

Code: Select all

magick mogrify -path path2/newfolder -format png -draw "image difference 0,0 0,0 'path2/watermark.png'" -negate *.png
Note that the watermark image should not be in the same folder as your other images, since all are png.

EDIT: I forgot the -negate. So I have modified the above to add it.
User avatar
GeeMack
Posts: 718
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: Watermark removal, please help.

Post by GeeMack »

BradLee wrote: 2017-04-11T09:30:21-07:00Now is there any was I can perform this on a whole folder of images?
If the watermark is the same size and location, in other words if you can use the same single image as your watermark template, and all your original images are the same size and orientation, you should be able to run a whole folder with a command something like this...

Code: Select all

magick *.png -set filename:f "%[t]" null: watermark.png ^
   -compose difference -layers composite -negate "cleaned\%[filename:f].png"
The results will be the same file names saved into a folder named "cleaned". Make sure you create the directory "cleaned" before you run it. You could adjust that command so it overwrites your originals by leaving out the "cleaned" directory name, but if something doesn't go as planned, you'll be sorry. ;) Also, if you'll be using this in a BAT script, make sure to change any single percent signs "%" to doubles "%%".

Edited to add: As fmw42 mentioned above, since your watermark image will also match "*.png", you may want to call it in from a location other than the folder with your working images.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Watermark removal, please help.

Post by fmw42 »

Always good to see multiple ways to do the same thing. IM 7 is more powerful than IM 6 and thus GeeMack's solution shows how a simple magick command can replace mogrify.
BradLee
Posts: 23
Joined: 2017-04-11T05:09:01-07:00
Authentication code: 1151

Re: Watermark removal, please help.

Post by BradLee »

All the images are the same size and the watermark is in the same location. (luckily)
I think I'll attempt this tomorrow when it's not so late and my head is a little less lumpy. :P

Thanks GeeMack, fmw42 and snibgo for your help.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Watermark removal, please help.

Post by snibgo »

You have 1200 images, each 791x1024 pixels. The solution starting with "magick *.png" needs 7.7 GB of memory just to read the images. If you run out of memory, this will be slow, or might fail.

The solution that starts with "magick mogrify" won't have that problem.
snibgo's IM pages: im.snibgo.com
BradLee
Posts: 23
Joined: 2017-04-11T05:09:01-07:00
Authentication code: 1151

Re: Watermark removal, please help.

Post by BradLee »

@fmw42
Trying your code above gives me the following errors:

Code: Select all

magick mogrify -path C:\Users\Brad\Downloads\padded -format png -draw "image difference 0,0 0,0 'C:\Users\Brad\Downloads\watermark\watermark.png'" -negate *.png
mogrify: unable to open image '*.png': Invalid argument @ error/blob.c/OpenBlob/
3094.
mogrify: unable to open file `*.png' @ error/png.c/ReadPNGImage/4354.
Last edited by BradLee on 2017-04-12T05:59:04-07:00, edited 1 time in total.
BradLee
Posts: 23
Joined: 2017-04-11T05:09:01-07:00
Authentication code: 1151

Re: Watermark removal, please help.

Post by BradLee »

@GeeMack
Using your code above gives me the following errors:

Code: Select all

magick *.png -set filename:f "%[t]" null: watermark\watermark.png -compose difference -layers composite -negate "cleaned\%[filename:f].png"
magick: unable to open image '*.png': Invalid argument @ error/blob.c/OpenBlob/3
094.
magick: unable to open file `*.png' @ error/png.c/ReadPNGImage/4354.
magick: no image to apply a property "%w" @ warning/property.c/GetMagickProperty
Letter/2561.
magick: unknown image property "%w" @ warning/property.c/InterpretImagePropertie
s/3499.
magick: no image to apply a property "%h" @ warning/property.c/GetMagickProperty
Letter/2449.
magick: unknown image property "%h" @ warning/property.c/InterpretImagePropertie
s/3499.
magick: no image to apply a property "%m" @ warning/property.c/GetMagickProperty
Letter/2480.
magick: unknown image property "%m" @ warning/property.c/InterpretImagePropertie
s/3499.
Although I don't think I have the usage right at all?
BradLee
Posts: 23
Joined: 2017-04-11T05:09:01-07:00
Authentication code: 1151

Re: Watermark removal, please help.

Post by BradLee »

snibgo wrote: 2017-04-11T10:31:03-07:00 You have 1200 images, each 791x1024 pixels. The solution starting with "magick *.png" needs 7.7 GB of memory just to read the images. If you run out of memory, this will be slow, or might fail.

The solution that starts with "magick mogrify" won't have that problem.
I have 16GB RAM and the images are in folders of roughly 200 each, so running out of memory shouldn't be a problem if I can get either solution to work properly.
User avatar
GeeMack
Posts: 718
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: Watermark removal, please help.

Post by GeeMack »

BradLee wrote: 2017-04-11T17:31:56-07:00Using your code above gives me the following errors:
I can generate exactly that error by running the command in a directory with no .png files.
BradLee
Posts: 23
Joined: 2017-04-11T05:09:01-07:00
Authentication code: 1151

Re: Watermark removal, please help.

Post by BradLee »

@GeeMack,
I thought I ran it from the right directory?
Let me check again.

EDIT: My mistake. It works just fine.
I'm a bit of a dunce when it comes to command line stuff. :oops:

Thanks again for your help!
Post Reply