<h2 id="introduction">Introduction</h2>
<p>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 <em>Exif</em> 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 <em>anonymise</em> images sent to HPR.</p>
<p>As I was implementing this I realised that one piece of Exif data: <code>'Orientation'</code>, 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.</p>
<p>This short episode describes the journey I had learning about this issue and finding how to get round it.</p>
<h2 id="the-problem">The Problem</h2>
<p>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.</p>
<p>I later discovered that the orientation setting can be viewed with the <a href="https://www.exiftool.org/" title="ExifTool by Phil Harvey"><code>exiftool</code></a> command:</p>
<pre><code>$ exiftool -orientation testimage.jpg
Orientation                     : Rotate 90 CW</code></pre>
<p>You can find information about the <code>Orientation</code> tag on the <a href="https://www.exiftool.org/TagNames/EXIF.html" title="ExifTool: EXIF Tags">ExifTool web site</a>.</p>
<p>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 <code>'Rotate 90 CW'</code> causes it to be displayed after rotating 90° in the clockwise direction. The actual value for this setting is <strong>6</strong>.</p>
<p>The problem is that removing <u>all</u> the Exif data causes such an image to revert to its <em>raw</em> state as explained below.</p>
<h2 id="investigation">Investigation</h2>
<h3 id="demonstration">Demonstration</h3>
<p>It took me a little while to understand this problem because I couldn’t find a good explanation of what was going on.</p>
<p>I found a <a href="https://github.com/recurser/exif-orientation-examples" title="EXIF Orientation-flag example images">repository on GitHub</a> 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 (<code>Rotate 90 CW</code>), and then with the Exif metadata removed.</p>
<p><em>Original image with orientation 6</em><br />
<img src="https://hackerpublicradio.org/eps/hpr3564/P1020743_6_original_tn.JPG" alt="Original image with orientation 6" /></p>
<p><em>Same image with Exif stripped</em><br />
<img src="https://hackerpublicradio.org/eps/hpr3564/P1020743_6_stripped_tn.JPG" alt="Same image with Exif stripped" /></p>
<h3 id="methods-used-to-fix-this">Methods used to fix this</h3>
<p>I found and installed some tools:</p>
<ul>
<li><code>jpegexiforient</code> - reads or writes the Exif Orientation Tag</li>
<li><code>exifautotran</code> - transforms Exif files so that Orientation becomes 1</li>
<li><code>jpegtran</code> - lossless transformation of JPEG files</li>
</ul>
<p>Note that these only operate on JPEG images.</p>
<p>The <code>exifautotran</code> tool is a shell script that uses <code>jpegexiforient</code> to find the orientation and <code>jpegtran</code> to undo whatever rotation (or other transformation) has been defined.</p>
<p>Reading the <code>exifautotran</code> script helped me understand all of this, but I did not use these tools in the end.</p>
<p>In the script I had written to manage images I also needed to do other image operations:</p>
<ul>
<li>interrogate the image to find its size to determine whether a thumbnail was needed</li>
<li>make a thumbnail if necessary</li>
</ul>
<p>To do this I had started to use the <code>GraphicsMagick</code> package.</p>
<p>This package actually caters for the orientation transformation I wanted to perform <u>and</u> can handle many image types, not just JPEG.</p>
<p>The technique is to use the command <code>'gm convert'</code> with two options:</p>
<ul>
<li><code>-strip</code> - remove all profiles and text attributes from the image</li>
<li><code>-auto-orient</code> - orient (rotate) the image so it is upright; adjusts the image orientation so that it is suitable for viewing</li>
</ul>
<p>Example:</p>
<pre><code>gm convert -strip -auto-orient sideways_pic.jpg normal_pic.jpg

</code></pre>
<p>Running this on the images in question removed the Exif orientation after having rotated the pixels of the image to the <code>'Horizontal (normal)'</code> state.</p>
<h2 id="conclusion">Conclusion</h2>
<p>I have modified my picture management script to use this technique, and so far it seems to do the job perfectly. It has to be admitted that images with Exif orientation metadata are rare though.</p>
<p>The <code>GraphicsMagick</code> documentation indicates that the transformations needed to generate an upright image could cause problems with some images, so we will be alert to any issues. For the moment, it looks as if the problem is largely solved.</p>
<p>Personally, I gained several things from this journey of discovery:</p>
<ul>
<li>I ended up understanding images a bit better.</li>
<li>Using <code>exiftool</code> to examine these images helped me to understand the power of this tool<a href="#fn1" class="footnote-ref" id="fnref1" role="doc-noteref"><sup>1</sup></a>.</li>
<li>I also discovered that if opened the example image with <code>Gimp</code> it spotted the orientation issue and asked if I wanted it to perform the transformation discussed above.</li>
<li>I installed a KDE image tool called <code>ShowFoto</code> and it also reported the fact that the image existed in two forms, in the same way, and allowed Exif editing.</li>
</ul>
<h2 id="links">Links</h2>
<ul>
<li>Exif:
<ul>
<li><a href="https://en.wikipedia.org/wiki/Exif">Wikipedia article about Exif</a></li>
<li><a href="https://www.exiftool.org/">ExifTool by Phil Harvey</a>
<ul>
<li><a href="https://www.exiftool.org/TagNames/EXIF.html">EXIF tag names</a></li>
</ul></li>
<li><a href="https://github.com/recurser/exif-orientation-examples">Github repository of Exif orientation examples</a></li>
</ul></li>
</ul>
<ul>
<li>GraphicsMagick:
<ul>
<li><a href="https://en.wikipedia.org/wiki/GraphicsMagick">Wikipedia article about GraphicsMagick</a></li>
<li><a href="https://www.graphicsmagick.org/">GraphicsMagick main website</a></li>
</ul></li>
</ul>
<section class="footnotes" role="doc-endnotes">
<hr />
<ol>
<li id="fn1" role="doc-endnote"><p>For the record, removing all Exif data with <code>exiftool</code> is achieved with the command:</p>
<pre><code>exiftool -all= image.jpg</code></pre>
<a href="#fnref1" class="footnote-back" role="doc-backlink">↩︎</a></li>
</ol>
</section>
