An inspection reviewer may draw a region around a feature in an upright image viewer and send those crop coordinates to a batch job. The file can carry an EXIF Orientation tag that a viewer uses to display the pixels upright. Code that crops the stored pixel grid without applying that tag can extract a different area. A quarter turn can also swap the dimensions, so a bounding box may fit one orientation and exceed the other.

Decide which pixel grid the annotations refer to before processing the image. For a workflow that records coordinates from a normalized upright view, use the same orientation normalization in the batch job before checking the box or extracting pixels.

Test a tag with an asymmetric fixture

The synthetic fixture stores an 8-by-5 image with a small blue rectangle away from the center. Orientation value 6 tells the viewer to rotate the stored pixels 90 degrees clockwise for display. PNG keeps the colors exact in this test. The fixture applies the metadata after reopening the image and checks the resulting dimensions and the extracted region.

from io import BytesIO

from PIL import Image, ImageOps

orientation_tag = 274

raw = Image.new("RGB", (8, 5), "black")

raw.paste((40, 120, 240), (1, 2, 3, 5))

exif = raw.getexif()

exif[orientation_tag] = 6

buffer = BytesIO()

raw.save(buffer, format="PNG", exif=exif)

buffer.seek(0)

with Image.open(buffer) as stored:

assert stored.size == (8, 5)

assert stored.getexif()[orientation_tag] == 6

upright = ImageOps.exif_transpose(stored)

assert upright.size == (5, 8)

assert orientation_tag not in upright.getexif()

assert upright.getpixel((0, 1)) == (40, 120, 240)

crop = upright.crop((0, 1, 3, 3))

assert crop.size == (3, 2)

assert crop.getcolors() == [(6, (40, 120, 240))]

The raw rectangle has an exclusive right and lower boundary at coordinates 3 and 5. In the upright grid it occupies columns zero through two and rows one through two. The new image has width five and height eight, and the original orientation tag has been removed after the transform. A crop drawn around the upright rectangle contains six blue pixels. The pixel position and color assertions confirm meaning that a size check alone would miss.

Choose a coordinate space at the boundary of the workflow

Pillow expresses crop boxes as left, upper, right and lower coordinates. If the annotation tool stores a bottom-right pixel inclusively or records fractional display coordinates after zoom and pan, convert that representation as well. Orientation does not account for resize, aspect-ratio padding or a thumbnail viewport. Record dimensions and the image version that the reviewer actually annotated so a later export can reconstruct the expected grid.

When importing historic marks, distinguish marks created on raw pixels from those created on a display-normalized view. If only the EXIF tag and a rectangle are available, the tag cannot establish which view an annotation tool used. Read the export convention or compare known landmarks. If there is a confirmed raw-to-upright transform, it has to be applied to the region coordinates as well as the image before they can be used together. A second transform on an already upright derivative can also move the crop, especially when the derivative exports inherited metadata.

Build regression checks around visible landmarks

Use a non-square test image with an asymmetric feature so the expected pixel motion is observable. Include an untagged image and a known orientation-tagged input. If the capture pipeline receives mirrored orientations, include an example with a reflected landmark. Checking only the width and height can miss those cases, since horizontal reflection does not change the dimensions.

Save a crop preview next to the inspection receipt with the source identity, the coordinate convention and the dimensions used for validation. A reviewer should be able to see the intended feature in that preview. If an image is re-encoded or transferred between systems, compare both the recorded orientation handling and the visible location of the crop. The result can then be checked even when different image viewers apply the EXIF information at different stages.

Sources

Pillow ImageOps reference, exif_transpose

https://pillow.readthedocs.io/en/stable/reference/ImageOps.html#PIL.ImageOps.exif_transpose

Pillow handbook, coordinate system and orientation

https://pillow.readthedocs.io/en/stable/handbook/concepts.html#coordinate-system