« Mercedes-Benz Driving Event | Main | Paper crane anyone? »

Watermarking Redux

After a few failed attempts at actually watermarking my images, I had to abandon Gallery's built in support. This was primarily driven by problems that I encountered with Gallery using the same watermark on both to the original and the sized version of an image. This meant that the watermark, if fitted for the sized image, hardly covered any of the original image. If the watermark was instead sized for the original image, the sized version only got a small portion of the watermark, based on how big the original image was. It seems Gallery doesn't recreate the sized image after applying the watermark to the original image. The one behavior that I did like about Gallery was that it didn't look like it tried to watermark the thumbnail version of the image.

What I ended up doing was to use the general principal of what Gallery was doing, but tweak it a little. I started off creating a bunch of differently sized watermarks in Adobe Photoshop to closely match the size of my original images. I saved them using the png-24 format, used 30% opacity for my text, and made both horizontal and vertical versions. I used the pnm tools to convert the png into a pnm and an alpha only pnm.

I wrote a little script that performs each of the following actions on every image:


  • Determine size of source image

  • Pick either a horizontal or vertical watermark

  • Resize the watermark and its alpha file to match either the horizontal or vertical dimensions of the source image

  • Convert the source jpg to pnm

  • Combine the source image with the watermark using the alpha channel file

  • Convert the combines pnm back to jpg and transfer over any exif information contained in the original jpg

  • Clean up all the temporary files

In keeping with the Gallery behavior I don't watermark the thumbnail or highlight photo for a particular album.


This approach got me closer to what I wanted but is not without its problems. In particular, since all of my watermarking is based around the same basic watermark, it shows up better on some images than on others. Chris tried to find a way to mimic Adobe's invert color overlay scheme, but so far we've not found a way to do this with just the pnm tools. I'm sure if you used ImageMagick or GIMP, you could probably script it, but that seemed like it would take more time to figure out, and I'm willing to live with good enough. I figure unless I individually watermark every image, or go with a much more obtrusive watermark, what I have now is good enough for me.

One problem I've noticed since having watermarked my images is that if you change the highlight photo or album, Gallery seems to regenerate the thumbnail that gets used. Given that the source pictures are now watermarked, this regenerated thumbnail now carries along the watermark. The only way around this that I've found so far is to manually overwrite the highlight jpg with an unwatermarked version.

Below is a sample of the commands that end up getting run on an image to apply the watermark. I ended up using Perl to generate the script since it has great support for getting the size of an image and it made some of the logic of figuring out which watermark to use for resizing straight forward.

# Scale the watermark to the size of the photo.
# In this case I'm scaling up, but usually
# I take a larger watermark and scale it down.
pnmscale -xsize=1536 danielr-1232.pnm > wmscale.pnm
pnmscale -xsize=1536 danielr-1232.pnma > wmscale.pnma
# Convert the jpg to pnm.
jpegtopnm -exif=jpg.exif 2004_Hanscom.jpg > temp.pnm
# Combine the source and watermark.
pnmcomp -align=center -valign=middle -alpha=wmscale.pnma wmscale.pnm temp.pnm > comp.pnm
# Back to jpg.
pnmtojpeg --exif=jpg.exif --quality=95 comp.pnm > new.2004_Hanscom.jpg
# Uou could just overwrite the original in the
# line above, but I had them separate so that I
# could easily test out the process.
mv -f new.2004_Hanscom.jpg 2004_Hanscom.jpg

If someone else wants to use this same method, drop me a line and I can send you the script.