Use exiv2 or imagemagick to remove EXIF data from

2019-05-21 06:45发布

问题:

How can I pipe an image into exiv2 or imagemagick, strip the EXIF tag, and pipe it out to stdout for more manipulation?

I'm hoping for something like:

exiv2 rm - - | md5sum

which would output an image supplied via stdin and calcualte its md5sum.

Alternatively, is there a faster way to do this?

回答1:

Using exiv2

I was not able to find a way to get exiv2 to output to stdout -- it only wants to overwrite the existing file. You could use a small bash script to make a temporary file and get the md5 hash of that.

image.sh:

#!/bin/bash
cat <&0 > tmp.jpg  # Take input on stdin and dump it to temp file.
exiv2 rm tmp.jpg   # Remove EXIF tags in place.
md5sum tmp.jpg     # md5 hash of stripped file.
rm tmp.jpg         # Remove temp file.

You would use it like this:

cat image.jpg | image.sh

Using ImageMagick

You can do this using ImageMagick instead by using the convert command:

cat image.jpg | convert -strip - - | md5sum

Caveat:

I found that stripping an image of EXIF tags using convert resulted in a smaller file-size than using exiv2. I don't know why this is and what exactly is done differently by these two commands.

From man exiv2:

rm        Delete image metadata from the files.

From man convert:

-strip        strip image of all profiles and comments

Using exiftool

ExifTool by Phil Harvey

You could use exiftool (I got the idea from https://stackoverflow.com/a/2654314/3565972):

cat image.jpg | exiftool -all= - -out - | md5sum

This too, for some reason, produces a slightly different image size from the other two.

Conclusion

Needless to say, all three methods (exiv2, convert, exiftool) produce outputs with different md5 hashes. Not sure why this is. But perhaps if you pick a method and stick to it, it will be consistent enough for your needs.



回答2:

I tested with NEF file. Seems only exiv2 rm works best. exiftool and convert can't remove all metadata from .nef FILE.

Notice that the output file of exiv2 rm can no longer be displayed by most image viewers. But I only need the MD5 hash keeps same after I update any metadata of the .NEF file. It works perfect for me.