Skip to content

TIFF orientation tag handled differently from most other software when loading G4 TIFF files #4053

@ailintom

Description

@ailintom

Most image software handles TIFF orientation tags (tag 274) differently from Pillow when loading binary G4 TIFF images. This means that only images having the orientation tag == 1 (The 0th row represents the visual top of the image, and the 0th column represents the visual left-hand side) are loaded correctly (in the same way as in other software), other images should be corrected manually.
I am not sure whether Pillow regards the orientation tags while other software ignores or vice versa. At least they are loaded transposed.
I use Pillow-6.1.0 with Python 3.8 on Windows.

I've developed the following procedure in my script to load TIFF files correctly (basically I only have files with orientation==8 from my office copier machine, so I have not tested other orientations). I think this something similar should be part of the TIFFImagePlugin

from PIL import Image
from tkinter import filedialog, Tk
ORIENTATION = 274
root = Tk()
root.withdraw()
file_selected = filedialog.askopenfilename()
srcim = Image.open(filename)
orientation = srcim.tag[ORIENTATION][0]
format = os.path.splitext(filename)[1]
if format.lower() in [".tif", ".tiff"]:
     try:
        if orientation == 8:
            srcim = srcim.transpose(Image.ROTATE_90)
        elif orientation == 3:
            srcim = srcim.transpose(Image.ROTATE_180)
        elif orientation == 6:
            srcim = srcim.transpose(Image.ROTATE_270)
        elif orientation == 2:
            srcim = srcim.transpose(Image.FLIP_LEFT_RIGHT)
        elif orientation == 4:
            srcim = srcim.transpose(Image.FLIP_TOP_BOTTOM)
        elif orientation == 5:
            srcim = srcim.transpose(Image.ROTATE_90).transpose(Image.FLIP_LEFT_RIGHT)
        elif orientation == 7:
            srcim = srcim.transpose(Image.ROTATE_270).transpose(Image.FLIP_LEFT_RIGHT)
     except:
          print("Can't detect orientation")

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions