Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

jima (1016)

jima
  (email not shown publicly)
http://www.empty-handed.com/

Born 1968. Not dead yet.

Journal of jima (1016)

Wednesday September 06, 2006
10:58 AM

Removing EXIF data from JPEG images using PerlMagick

[ #30908 ]

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:

use Image::Magick;

my($i) = Image::Magick->new();
my($j) = Image::Magick->new();

$i->Read('image-with-exif.jpg');
$j->Set('size', $i->Get('width').'x'.$i->Get('height'));
$j->Read('NULL:white');

$resp = $j->Composite( image => $i, compose => Copy );  # you can check $resp for errors

$j->Write('image-without-exif.jpg');

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!