23

I have a PHP photo sharing application in which user-uploaded images are resized into various thumb formats using ImageMagick.

As a seemingly "smart" way to save on file size, I am stripping exif info from these thumbs as follow:

$imagick = new Imagick($image);
$imagick->stripImage();
$imagick->writeImage($image);

This works. It does remove the EXIF info, where a thumbs of 30KB saves 12KB and becomes 18KB. A significant saving when showing many of such thumbs on a single page.

The problem however is that it works a little too well. The resulting images seem to lose a lot of color information and look "flat" compared to their non-stripped versions.

Based on my research so far, my theory is that one or both of the following is true:

  • Imagick throws away essential color profile information as part of the stripping process
  • Imagick recompresses the image upon saving it, losing quality

Regardless of the cause of the problem, I'm looking for a way to remove EXIF information in such a way that it does not affect the image quality or color itself.

Is this even possible?

Update:

Based on Gerald Schneider's answer, I tried enforcing the quality setting to 100% prior to "stripping" the image:

$imagick = new Imagick($image);
$imagick->setCompression(imagick::COMPRESSION_JPEG);
$imagick->setCompressionQuality(100);
$imagick->stripImage();
$imagick->writeImage($image);

Unfortunately, the problem remains. Below is example output where despite setting the quality to 100%, images are still flattened.

enter image description here

Fer
  • 4,116
  • 16
  • 59
  • 102
  • 2
    Somehow I doubt that removing the EXIF reduces the file size that much. I guess the file size reduction is rather due to the recompression, maybe with lower quality than before. Did you try using `getCompressionQuality()` and `setCompressionQuality()`? – Gerald Schneider Nov 30 '12 at 13:20
  • @GeraldSchneider this depends. I have seen files with literally hundreds of kilobytes (almost half a meg in some extreme cases) of EXIF data. Stripping the images of EXIF is always a good idea. – mingos Nov 30 '12 at 13:22
  • That might be true for a image straight out of a camera, but for a generated thumbnail? – Gerald Schneider Nov 30 '12 at 13:23
  • Look here http://stackoverflow.com/questions/2654281/how-to-remove-exif-data-without-recompressing-the-jpeg – piotrekkr Nov 30 '12 at 13:24
  • @GeraldSchneider It is perfectly common for EXIF to take up 10KB or more on an image. On a 10MB image, it is neglectible, on a 20KB thumbnail, it is 50%. – Fer Nov 30 '12 at 13:56
  • @GeraldSchneider I have updated my answer to show findings of your suggestion to explicity set the compression quality. – Fer Nov 30 '12 at 14:05
  • According to the comments in the manual, you should use `setImageCompression` and `setImageCompressionQuality` in newer versions of php. Does that result in better images? – jeroen Nov 30 '12 at 14:12
  • @jeroen Thanks, but in results in the same effect. – Fer Nov 30 '12 at 14:18
  • When Imagemagick opens a jpg and does nothing then saves it the image will be compressed. Why are you not removing the data when the thumb is created in the first place? – Bonzo Nov 30 '12 at 19:58
  • Check this page about jpg and Imagemagick: http://www.imagemagick.org/Usage/formats/#jpg – Bonzo Nov 30 '12 at 22:00
  • +bonzo Because it also removes color information, as discussed above. – Fer Dec 01 '12 at 00:04
  • 1
    If a few KB really matter to you, decrease the compression ratio of the thumbnails. Or try opening the original, resize, then copy it into a new image and save. That may remove the unnecessary EXIF data as long as the image library properly translates the color profile. You may have to explicitly copy the color profile over, or use a better library. If you REALLY care, dive into the JPEG spec and manually remove the EXIF data on a trial and error basis until you keep the parts that matter. http://www.media.mit.edu/pia/Research/deepview/exif.html – Levi Dec 03 '12 at 01:30
  • This: http://stackoverflow.com/questions/3614925/remove-exif-data-from-jpg-using-php –  Dec 06 '12 at 07:31
  • Do you have an image available with the metadata and profiles enabled to test with? – MatsLindh Dec 13 '12 at 22:22

3 Answers3

32

Consider keeping the ICC profile (which causes richer colors) while removing all other EXIF data:

  1. Extract the ICC profile
  2. Strip EXIF data and image profile
  3. Add the ICC profile back

In PHP + imagick:

$profiles = $img->getImageProfiles("icc", true);

$img->stripImage();

if(!empty($profiles))
    $img->profileImage("icc", $profiles['icc']);

(Important note: using the ImageMagick 3.1.0 beta, the result I got from getImageProfiles() was slightly different from the documentation. I'd advise playing around with the parameters until you get an associative array with the actual profile(s).)

For command line ImageMagick:

convert image.jpg profile.icm
convert image.jpg -strip -profile profile.icm output.jpg

Images will get recompressed of course if you use ImageMagick, but at least colors stay intact.

Hope this helps.

Robbert
  • 5,063
  • 6
  • 36
  • 44
  • 1
    Thanks, Robbert. I tried a bunch of other techniques, but this was the only one that gave me the results I was after. I'm working with ImageMagick from the command line, so I had to translate a bit: 1) Save Profile to file `convert image.jpg profile.icm` 2) Strip profile from image, then reapply from file `convert image.jpg -auto-orient -strip -profile profile.icm output.jpg` – Chad von Nau Sep 22 '13 at 23:09
  • Glad to be of help, Chad. Thanks for supplying the command line alternative - I've added your code to the answer. – Robbert Sep 24 '13 at 15:35
  • Cool, just fyi, the -auto-orient for orienting photos with exif rotation data and isn't necessary for the command to work. I meant to take it out of the comment, but my edit window expired! – Chad von Nau Sep 24 '13 at 19:16
  • I was wondering what that was for, forgot to Google it. Thanks! – Robbert Sep 24 '13 at 21:06
  • I want to remove the metadata from java(**jmagick**).Any help? – Querier Sep 29 '16 at 07:17
  • The above image magick command is not working [this image](http://capitalinvestments.ru/wp-content/uploads/2016/04/warren-buffett-reuters-110213.jpg) . So much color loss in the metadata removed image. – Querier Sep 30 '16 at 06:19
1

Having made similar changes to MIME types in file headers that were incorrectly stored, I'd suggest you verify the length of the EXIF data via the standard tools, and then "Zero" the data manually using multibyte string functions.

EXIF can only be a maximum of 64KB in a JPEG file, however I'm not positive if it's exacly 64KB, so I would begin with this.

oucil
  • 4,211
  • 2
  • 37
  • 53
-2

GIMP (another opensource image editor) also allows to remove EXIF, IPTC, XMP, thumbnail, ICC (color profile), etc meta data without loosing image/picture quality.

After opening the image in GIMP, Choose "Export" / "Export As" etc option, then select/unselect what meta data you want to remove/keep.

To keep original image quality you must select (tick mark) the option "Use quality settings from original image", which is shown under the "Quality" sliding-bar in top.

I usually like to keep ICC (save color profile meta data) option selected, to keep good color quality, with a colored photo.
Other options (Save EXIF, Save IPTC, Save XMP, Save thumbnail, etc) i usually unselect.

More info: here.

atErik
  • 923
  • 2
  • 13
  • 24