Removing EXIF data from JPEG images using PerlMagick

One issue that came up when we were testing the original version of this software was that metadata was being kept in images that were created by the system. Small "slices" of the original images uploaded by players were passed along to subsequent players, for them to use in their own creations, and any existing EXIF thumbnail images in the original image file were accidentally also being passed along.

After some futzing around with ImageMagick http://www.imagemagick.org/ I eventually settled on using the jpegtran tools that come installed on just about every flavor of Linux. But I never gave up on a possible PerlMagick solution, and now I've finally stumbled upon the correct combination of incantations that allow you to create an image with no EXIF data. Since the documentation for ImageMagick is quite sparse, I thought I would record my discovery here.

I suspected that copying an image from one Image::Magick object to another would probably not copy the EXIF data, and it took me a while to stumble on the Composite() method which in fact works exactly as I suspected it would. Here's my code:

A couple of things that might not be obvious to the ImageMagick novice:

The $j->Read() function call is needed to create an image in the $j variable. Apparently you need to have some image in your destination variable to copy stuff onto.

The NULL:white string is what's known in ImageMagick as a pseudo-image format. Other formats, useful for creating gradients, steganographic images, plasma backgrounds, etc. can be found in this page in the ImageMagick documentation: http://www.imagemagick.org/script/formats.php#pseudo

Some investigation with the resulting images using the Image::ExifTool module indicates that there is no resulting EXIF data in the new JPEG image. Mission accomplished!