HPR3564: Removing EXIF data from an image




Hacker Public Radio show

Summary: Introduction I’m writing a script to process image files sent in by HPR hosts with their shows. One of the things the script does is to strip Exif metadata from such images. That’s because this metadata may contain details that could identify the creator of the image - their camera, their location, and other things. Many people will be alert to this, but in case anything slips through it seems a courtesy to anonymise images sent to HPR. As I was implementing this I realised that one piece of Exif data: 'Orientation', can’t just be removed. Sometimes images are created with a particular orientation by the camera but are written with an Exif orientation setting that shows another orientation. If this is just removed the image might be shown wrongly. This short episode describes the journey I had learning about this issue and finding how to get round it. The Problem A show was sent in early March 2022 which had three images with orientation values in the Exif metadata. They had apparently been taken with one orientation but were being rotated for viewing. I later discovered that the orientation setting can be viewed with the exiftool command: $ exiftool -orientation testimage.jpg Orientation : Rotate 90 CW You can find information about the Orientation tag on the ExifTool web site. The actual image in this case is rotated 90° anti-clockwise (the top of the image is to the left) and this needs to be reversed. The setting 'Rotate 90 CW' causes it to be displayed after rotating 90° in the clockwise direction. The actual value for this setting is 6. The problem is that removing all the Exif data causes such an image to revert to its raw state as explained below. Investigation Demonstration It took me a little while to understand this problem because I couldn’t find a good explanation of what was going on. I found a repository on GitHub which would take a picture and generate all of the possible Exif orientations from it. I used it to generate pictures from one (a thumbnail) I used in an old HPR show. Here’s the original picture with an orientation setting of 6 (Rotate 90 CW), and then with the Exif metadata removed. Original image with orientation 6 Same image with Exif stripped Methods used to fix this I found and installed some tools: jpegexiforient - reads or writes the Exif Orientation Tag exifautotran - transforms Exif files so that Orientation becomes 1 jpegtran - lossless transformation of JPEG files Note that these only operate on JPEG images. The exifautotran tool is a shell script that uses jpegexiforient to find the orientation and jpegtran to undo whatever rotation (or other transformation) has been defined. Reading the exifautotran script helped me understand all of this, but I did not use these tools in the end. In the script I had written to manage images I also needed to do other image operations: