Skip to content

Commit

Permalink
Fix resizing for images with EXIF orientation (#2468)
Browse files Browse the repository at this point in the history
* Fix resizing for images with EXIF orientation

* Added test for asymmetric resize for exif images

---------

Co-authored-by: Tanishq <tanishq@levels.fyi>
  • Loading branch information
2 people authored and Keats committed Jun 20, 2024
1 parent 954f1ce commit 6a25b62
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
5 changes: 2 additions & 3 deletions components/imageproc/src/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ impl ImageOp {
return Ok(());
}

let mut img = image::open(&self.input_path)?;
let img = image::open(&self.input_path)?;
let mut img = fix_orientation(&img, &self.input_path).unwrap_or(img);

let img = match self.instr.crop_instruction {
Some((x, y, w, h)) => img.crop(x, y, w, h),
Expand All @@ -49,8 +50,6 @@ impl ImageOp {
None => img,
};

let img = fix_orientation(&img, &self.input_path).unwrap_or(img);

let f = File::create(&self.output_path)?;
let mut buffered_f = BufWriter::new(f);

Expand Down
22 changes: 22 additions & 0 deletions components/imageproc/tests/resize_image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,3 +248,25 @@ fn check_img(img: DynamicImage) -> bool {
// bottom right is white
&& img.get_pixel(15, 15).channels() == [255, 255, 255, 255]
}

#[test]
fn asymmetric_resize_with_exif_orientations() {
// No exif metadata
image_op_test("exif_0.jpg", "scale", Some(16), Some(32), "auto", "jpg", 16, 32, 16, 16);
// 1: Horizontal (normal)
image_op_test("exif_1.jpg", "scale", Some(16), Some(32), "auto", "jpg", 16, 32, 16, 16);
// 2: Mirror horizontal
image_op_test("exif_2.jpg", "scale", Some(16), Some(32), "auto", "jpg", 16, 32, 16, 16);
// 3: Rotate 180
image_op_test("exif_3.jpg", "scale", Some(16), Some(32), "auto", "jpg", 16, 32, 16, 16);
// 4: Mirror vertical
image_op_test("exif_4.jpg", "scale", Some(16), Some(32), "auto", "jpg", 16, 32, 16, 16);
// 5: Mirror horizontal and rotate 270 CW
image_op_test("exif_5.jpg", "scale", Some(16), Some(32), "auto", "jpg", 16, 32, 16, 16);
// 6: Rotate 90 CW
image_op_test("exif_6.jpg", "scale", Some(16), Some(32), "auto", "jpg", 16, 32, 16, 16);
// 7: Mirror horizontal and rotate 90 CW
image_op_test("exif_7.jpg", "scale", Some(16), Some(32), "auto", "jpg", 16, 32, 16, 16);
// 8: Rotate 270 CW
image_op_test("exif_8.jpg", "scale", Some(16), Some(32), "auto", "jpg", 16, 32, 16, 16);
}

0 comments on commit 6a25b62

Please sign in to comment.