1

I want to find a method to add and remove a known watermark from image with Java.

I have an image1.png and a watermark watermark.png

I want to find a method to addWatermark(image1, watermark) --> image2.png

And then I want to recover the origin image like this removeWatermark(image2, watermark) --> image1.png

Can anyone give me a suggestion for this problem?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Robotic Vn
  • 437
  • 2
  • 7
  • 20
  • 6
    The idea of a watermark is that it is hard/impossible to remove. Meaning if you can 'undo' a watermark, it defeats its purpose. So the sensible solution would be a to store the original image somewhere secure, and obtain that when you 'undo' a watermark. To answer your question though; You can create a watermark mask and apply a pixel-based operation (such as blending operations). You can undo these operations by reaplying that mask with its opposite effect (addition vs subtraction, division vs multiplication, or simply xor them etc.) – n247s Aug 15 '22 at 07:48
  • Why do you need to remove a watermark anyway? – Olivier Aug 15 '22 at 08:02
  • @n247s Could you give me a snippet of code for a simple add-subtract of two images? – Robotic Vn Aug 15 '22 at 08:37
  • @Olivier I want to store my watermarked image in a public location but I made an app and want my user can view non-watermark images in the app – Robotic Vn Aug 15 '22 at 08:39
  • 5
    @RoboticVn that's it's bad design/architecture. Is it a school project or something ? You need to store two versions of the same picture : one with waterwark (public) and one without (private). – KeatsPeeks Aug 15 '22 at 09:46
  • @KeatsPeeks No, I only need to store the watermarked image in public and I keep my watermark in private (my app) – Robotic Vn Aug 16 '22 at 00:24
  • I cannot create a snippet for you unfortunatly (time restrictions), but a word of caution regarding the 'watermark mask' method. It is verry easy to figure out what the mask is by comparing several watermarked images, or by simply uploading a completely white or black image. Its simular to encrypting with [cesar-cipher](https://en.m.wikipedia.org/wiki/Caesar_cipher). It might seem impressive, but its easy to reverse engeneer even if you don't know all implementation details. As multiple people have suggested, its way easier and more secure to store the original image instead. – n247s Aug 16 '22 at 11:52

1 Answers1

2

To overlay two images you can just use the following java code:


File path = ... // base path of the images and the result

// load images
BufferedImage image = ImageIO.read(new File(path, "image.png"));
BufferedImage watermark = ImageIO.read(new File(path, "watermark.png"));

// create the new image, size is the max. of both image sizes
int w = Math.max(image.getWidth(), watermark.getWidth());
int h = Math.max(image.getHeight(), watermark.getHeight());
BufferedImage combined = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);

// now just paint both images, preserving the alpha channels
Graphics g = combined.getGraphics();
g.drawImage(image, 0, 0, null);
g.drawImage(watermark, 0, 0, null);

g.dispose();

// Save this as a new image
ImageIO.write(combined, "PNG", new File(path, "combined.png"));

The removal of watermarks, as the previous speakers have already mentioned, is not that easy.