Skip to content

Commit 0b9c58c

Browse files
committed
Apply EXIF orientation when converting images with ImageMagick
Photos taken on phones store pixels unrotated with an EXIF Orientation tag. Converting them to formats where the tag is lost or ignored (png, webp, ...) produced sideways images. Add -auto-orient so the pixels are rotated according to the tag before writing the output.
1 parent 393441f commit 0b9c58c

2 files changed

Lines changed: 20 additions & 0 deletions

File tree

src/converters/imagemagick.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,10 @@ export function convert(
468468
outputArgs.push("-background", "white", "-alpha", "remove");
469469
}
470470

471+
// Apply EXIF orientation so photos (e.g. from phones) don't end up sideways
472+
// when converted to formats where the orientation tag is lost or ignored
473+
outputArgs.push("-auto-orient");
474+
471475
return new Promise((resolve, reject) => {
472476
execFile(
473477
"magick",

tests/converters/imagemagick.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,3 +163,19 @@ test("convert respects emf as input filetype", async () => {
163163
);
164164
expect(loggedMessage).toBe("stdout: Fake stdout");
165165
});
166+
167+
test("convert applies EXIF auto-orient", async () => {
168+
const mockExecFile: ExecFileFn = (
169+
_cmd: string,
170+
_args: string[],
171+
callback: (err: ExecFileException | null, stdout: string, stderr: string) => void,
172+
) => {
173+
calls.push(_args);
174+
callback(null, "", "");
175+
};
176+
177+
const result = await convert("input.jpg", "jpg", "png", "output.png", undefined, mockExecFile);
178+
179+
expect(result).toBe("Done");
180+
expect(calls[0]).toEqual(expect.arrayContaining(["input.jpg", "-auto-orient", "output.png"]));
181+
});

0 commit comments

Comments
 (0)