Skip to content

autoOrient misbehavior when combined with additional rotation #4425

@mbklein

Description

@mbklein

Possible bug

Is this a possible bug in a feature of sharp, unrelated to installation?

  • Running npm install sharp completes without error.
  • Running node -e "require('sharp')" completes without error.

If you cannot confirm both of these, please open an installation issue instead.

Are you using the latest version of sharp?

  • I am using the latest version of sharp as reported by npm view sharp dist-tags.latest.

If you cannot confirm this, please upgrade to the latest version and try again before opening an issue.

If you are using another package which depends on a version of sharp that is not the latest, please open an issue against that package instead.

What is the output of running npx envinfo --binaries --system --npmPackages=sharp --npmGlobalPackages=sharp?

  System:
    OS: macOS 15.5
    CPU: (12) arm64 Apple M2 Max
    Memory: 231.33 MB / 32.00 GB
    Shell: 5.9 - /opt/homebrew/Cellar/zsh/5.9/bin/zsh
  Binaries:
    Node: 22.16.0 - ~/.asdf/installs/nodejs/22.16.0/bin/node
    Yarn: 1.22.11 - ~/.asdf/shims/yarn
    npm: 10.9.2 - ~/.asdf/plugins/nodejs/shims/npm
    pnpm: 10.12.1 - ~/.asdf/installs/nodejs/22.16.0/bin/pnpm
    bun: 1.1.14 - ~/.asdf/shims/bun
  npmPackages:
    sharp: ^0.34.3 => 0.34.3 

Does this problem relate to file caching?

The default behaviour of libvips is to cache input files, which can lead to EBUSY or EPERM errors on Windows.
Use sharp.cache(false) to switch this feature off.

  • Adding sharp.cache(false) does not fix this problem.

Does this problem relate to images appearing to have been rotated by 90 degrees?

Images that contain EXIF Orientation metadata are not auto-oriented. By default, EXIF metadata is removed.

  • To auto-orient pixel values use the parameter-less rotate() operation.

  • To retain EXIF Orientation use keepExif().

  • Using rotate() or keepExif() does not fix this problem.

What are the steps to reproduce?

  1. Load any image.
  2. autoOrient() to rectify the image based on the EXIF orientation value.
  3. Apply an extract() transformation.
  4. Apply a rotate() transformation.

What is the expected behaviour?

The extraction and rotation would work just as they would if the source image were already in EXIF orientation 1.

Please provide a minimal, standalone code sample, without other dependencies, that demonstrates this problem

The mbklein/auto-orient-test repo demonstrates the issue. Follow the instructions in the README and then check the output directory. (It's easiest to get a visual sense of what's happening if you can view them in a 4x32 grid sorted by filename.)

I would expect all of the output to look like the 1-noauto results (with the 1 in the center of the image replaced with the correct source orientation), but even the rotated 1-auto results are incorrect, despite not needing any reorienting to begin with. It's hard to tell exactly what's wrong with the auto-crop results, but the rotated auto-full results make it clear that there's some confusion about the bounding box. The rotated output is being constrained to the extract region's width and height instead of expanding the box to allow the rotated image to fit.

Please provide sample image(s) that help explain this problem

The issue arises on every image I've tried, but the proof of concept above uses the examples from recurser/exif-orientation-examples

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions