4 Replies Latest reply on Apr 15, 2018 1:25 PM by alanterra

    Restore photo orientation

    alanterra Level 2

      I needed to restore the orientation to a bunch of photos, and I couldn't find anything about this (admittedly, with a brief search). So here is recipe in case others need to do it.

       

      Ingredients:

      Lightroom Classic

      Jeffrey's Data Explorer Plugin

      Phil Harvey's exiftool

      John Ellis' AnyFilter Plugin (optional)

       

      When you import photos into Lightroom, they are oriented according to metadata that the camera adds to the photo. One place this is recorded is the Orientation EXIF field. I am using Canon cameras, and some of my cameras, but not all, also record this data in the Camera Orientation field. (Confusingly, the Camera Orientation doesn't have a "Rotate 180" value.)

       

      Values for the Orientation Field are:

      1 = Horizontal (normal)

      2 = Mirror horizontal

      3 = Rotate 180

      4 = Mirror vertical

      5 = Mirror horizontal and rotate 270 CW

      6 = Rotate 90 CW

      7 = Mirror horizontal and rotate 90 CW

      8 = Rotate 270 CW

       

      The data explorer plugin can be used to restore a photo to "sensor orientation", but it can't be used to figure out the original camera orientation.

       

      The first thing is to restore all of the photos to "sensor orientation." Use Data Explorer to break your photos up into collections depending on the "Orientation with Respect to Image Sensor", and then rotate the photos in each collection appropriately (if they are rotated 90° CW, apply a 90° CCW rotation, etc.)

       

      The next thing to do is to find the original orientation. If all of your photos have the "Camera Orientation" metadata, it seems that you can use the Any Filter plugin to find them, which is quite straightforward. E.g., do a search for "Aggregate Text" "Searchable EXIF" "contains" "Rotate 90 CW". However, my photos from my old Canon 1Ds don't have the "Camera Orientation" metadata, and it seems that Any Filter can't search on the "Orientation" metadata (probably because the values of Orientation are integers, not strings).

       

      So I used the following command in exiftool to set a subject keyword according to the original orientation:

       

        % cd <the folder with all your photos in it>

        % exiftool '-subject+<$orientation' -ext xmp -overwriteoriginalinplace .

       

      This sets a keyword that is derived from the value of the orientation field for the sidecar files in the directory. (Replace "xmp" with "TIFF", "psd", etc to change those file types). Be careful, the "-overwriteoriginalinplace" flag will do exactly that -- make sure you have a backup before doing this command.

       

      Now you just need to read the metadata in Lightroom, and sort by the keyword values and restore the camera's suggested orientation.

       

      Hope this helps.

       

      A

        • 1. Re: Restore photo orientation
          johnrellis Most Valuable Participant

          I don't think your recipe works in the case when LR has saved metadata back to the photos.  Here's a straightword but tedious method that just involves just Any Filter or just Data Explorer.

           

          First, some background. There are three different places the orientation of a photo can be stored:

           

          - EXIF:Orientation is set by most cameras.  LR will overwrite this field when you do Metadata > Save Metadata To File or you have set the option Catalog Settings > Metatadata > Automatically Write Changes Into XMP.

           

          - EXIF MakerNotes may have an orientation field set by the camera. E.g. some Canons and Sonys store MakerNotes:CameraOrientation. LR doesn't read or write this field.

           

          - The LR catalog's Orientation field contains the current orientation of the photo within LR. It is initialized on import from EXIF:Orientation but changes when you do Photo > Rotate Right / Left.

           

          Next, there are two different cases to consider, each with a different recipe:

           

          A. LR hasn't written metadata back to the photos with Save Metadata To File or Automatically Write Changes.

           

          1. Reset all photos back to 0 degrees rotation (horizontal / landscape). Use Any Filter with the criterion "Orientation = 90" to find all photos with 90 degrees rotation, and then use Photo > Rotate Left to rotate them back to 0.  Then use Any Filter to find all photos with 180 degrees and rotate them twice.  Similarly for 270 degrees.

           

          2. Set the rotation of all photos to that set by the camera  (still stored in EXIF:Orientation). Use Any Filter with the criterion "ExifTool Field = Rotate 90 CW | Orientation" to find all photos rotated 90 degrees by the camera.  Use Photo > Rotate Right to rotate those photos back to 90 degrees.  Then repeat for "Rotate 180" and "Rotate 270 CW".

           

          B. LR has written metadata back to photos with Save Metadata To File or Automatically Write Changes. In this case, you'll only be able to restore the camera rotation for those cameras that have also stored the rotation in a Makernotes field (e.g.  Canon's Makernotes:CameraOrientation), since LR has overwritten the original EXIF:Orientation field set by the camera.

           

          1. Find all photos that have a MakerNotes:CameraOrientation field. Use Any Filter with the criterion "ExifTool Field isn't empty | CameraOrientation".

           

          2. Reset the rotation of all the photos from step 1 to 0 degrees (horizontal / landscape). Use Any Filter with the criterion "Orientation = 90" to find all photos with 90 degrees rotation, and then use Photo > Rotate Left to rotate them back to 0.  Then use Any Filter to find all photos with 180 degrees and rotate them twice.  Similarly for 270 degrees..

           

          3. Set the rotation of all photos from step 1 to that set by the camera  (still stored in MakerNotes:CameraOrientation). Use Any Filter with the criterion "ExifTool Field = Rotate 90 CW | CameraOrientation" to find all photos rotated 90 degrees by the camera.  Use Photo > Rotate Right to rotate those photos back to 90 degrees.  Then repeat for "Rotate 180" and "Rotate 270 CW".

           

          ------------------

          You can use a similar recipe with Data Explorer, which also lets you search ExifTool fields.

           

          Finally, note that this recipe omits the case of mirroring (flipping photos in LR horizontally or vertically).  The same general approach works -- first identify and undo LR-applied mirroring, then identify and undo LR-applied rotation.

          • 2. Re: Restore photo orientation
            alanterra Level 2

            Hi John

             

            I hadn't realized that Any Filter had an orientation filter, I see now how it works. This means, as you say, that I can accomplish my goal with just your plugin. Thanks for the recipes.

             

            I don't know why you think that Lightroom writes the Orientation field when you synchronize metadata. In my experience (sample size ~6,000 photos in one catalog with "Automatically write metadata changes" on), it does not. In any event, even if it did, for raw files the original value would be available in the raw file. (For others following this conversation, Lightroom does not, in general, write changes to raw files, but only to the xmp sidecar.)

             

            If it is the case that Lightroom has changed the data in the .xmp sidecar, you can add the flag

             

                % exiftool <flags>  -ext xmp -tagsFromFile %f.CR2 <directory>

             

            which will read the flags from the corresponding .CR2 file but operate on the .xmp file.

             

            I have not experimented with non-raw file formats. Again, if you save your dngs, TIFFs, psds and jpgs with the same name as the raw files, and put them in the same directory, the -tagsFromFile trick can be used.

             

            Again, thanks for your awesome plugin.

             

            A

            • 3. Re: Restore photo orientation
              johnrellis Most Valuable Participant

              I don't know why you think that Lightroom writes the Orientation field when you synchronize metadata.

              Try the experiment: Pick a non-raw photo, rotate it, and do Metadata > Save Metadata To File. Then do

              exiftool -exif:orientation file

              and observe that EXIF:Orientation has changed.

               

              For raws, the orientation is written to the sidecar (in XMP:Orientation), not to the raw. So as you hinted and I neglected to realize, for raws you can always recover the original camera orientation by reading EXIF:Orientation from the raw.  But for non-raws, if LR has written metadata back to the file, you'll need a manufacturer-specific MakerNotes field to recover the original orientation.

               

              Thus, the conditions for my two recipes are a little different:

               

              A. The photos are either raw, or for non-raws, LR hasn't written metadata back to the photos with Save Metadata To File or Automatically Write Changes.

               

              B. The photos are non-raws and LR has written metadata back to photos with Save Metadata To File or Automatically Write Changes.

               

              The actual recipes for A and B remain the same. (The Any Filter ExifTool criterion always reads metadata from the raw file, not its sidecar.)

              • 4. Re: Restore photo orientation
                alanterra Level 2

                Something strange was going on with my computer. I see the behavior you describe now. (Scratching my head in puzzlement).

                 

                A