Fred's ImageMagick Scripts



    Licensing:

    Copyright © Fred Weinhaus

    My scripts are available free of charge for non-commercial (non-profit) use, ONLY.

    For use of my scripts in commercial (for-profit) environments or non-free applications, please contact me (Fred Weinhaus) for licensing arrangements. My email address is fmw at alink dot net.

    If you: 1) redistribute, 2) incorporate any of these scripts into other free applications or 3) reprogram them in another scripting language, then you must contact me for permission, especially if the result might be used in a commercial or for-profit environment.

    Usage, whether stated or not in the script, is restricted to the above licensing arrangements. It is also subject, in a subordinate manner, to the ImageMagick license, which can be found at: http://www.imagemagick.org/script/license.php

    Please read the Pointers For Use on my home page to properly install and customize my scripts.

UNWATERMARK


Removes a larger (uniform colored) watermark from a small font text image.

Download Script

last modified: July 08, 2024



USAGE: unwatermark [-c close] [-d dilate] infile outfile
USAGE: unwatermark [-h|-help]

-c close close morphology disk kernel size to extract watermark from image; float>0; default=3 -d dilate dilate morphology disk kernel size on watermark mask image; float>0; default=1.5 -s save save intermediate images: watermark (w), mask (m) or both (b) as PNG format named after input.

PURPOSE: To remove a larger (uniform colored) watermark from a small font text image.

DESCRIPTION: UNWATERMARK removes a larger (uniform colored) watermark from a small font text image.

ARGUMENTS:

-c close ... CLOSE morphology disk kernel size to extract watermark from the image. Values are floats>0. The default=3. The kernel diameter should be larger than the smaller font text and much smaller than the larger uniform colored watermark text. (diameter = 2*radius = 2*3*close (approx.); diameter for close=3 is 2*3*3=18)

-d dilate ... DILATE morphology disk kernel size on watermark mask image. Values are floats>=0. The default=1.5

-s save ... SAVE intermediate images; choices are: watermark (w), mask (m) or both (b) as PNG format named after input. Default is no save images.

REQUIREMENTS: Requires IM 7 in order to get the median color of just the watermark in the image.

REFERENCE: Christoph Rackwitz's Python Code at: https://stackoverflow.com/questions/78130983/opencv-remove-watermark/78672690#78672690

CAVEAT: No guarantee that this script will work on all platforms, nor that trapping of inconsistent parameters is complete and foolproof. Use At Your Own Risk.


EXAMPLES


Example 1

Original Image
(source)

Arguments:
-s watermark
Result:
Watermark:


Example 2

Original Image
(source)

Arguments:
-s watermark
Result:
Watermark:


Example 3

Original Image
(source)

Arguments:
-s watermark
Result:
Watermark:


What the script does is as follows:

  • Reads input
  • Converts a copy of the image to grayscale
  • Applies a grayscale close morphology to remove all small text and leave the grayscale watermark only
  • Applies Otsu thresholding to the watermark in grayscale and inverts to form a mask image
  • Gets the median color of watermark from the input using the mask
  • Colorizes the mask with the median color to form a colored version of the watermark
  • Computes a correction factor from the grayscale version of the image and the mask
    as the inverse of the grayscale median value
  • Corrects the grayscale image by multiplying with the correction factor,
    then replaces the gray image with the corrected gray image where the mask is white
    and uses the input image where the mask is black
  • Write the output