SixLabors / ImageSharp

:camera: A modern, cross-platform, 2D Graphics library for .NET
https://sixlabors.com/products/imagesharp/
Other
7.31k stars 846 forks source link

Exif rotation and save #2693

Closed bertasoft closed 5 months ago

bertasoft commented 5 months ago

Prerequisites

ImageSharp version

3.1.3

Other ImageSharp packages and versions

no package

Environment (Operating system, version and so on)

Windows 11

.NET Framework version

NET 8

Description

Exif rotation and save

3.1.3

I have some Exif image to adjust but the library don't give the expected result.

Before Screenshot 2024-03-09 101858

After, the library make the rotation Screenshot 2024-03-09 101919

But it doesn't save correctly Screenshot 2024-03-09 101953

In the attachment the image that i used

Thanks

Steps to Reproduce

Look the description

Images

original

JimBobSquarePants commented 5 months ago

I’m not sure what you are expecting here. The image was rotated to match the exif orientation.

bertasoft commented 5 months ago

I'm expected that the rotated is like this

Rotated

JimBobSquarePants commented 5 months ago

That's an incorrect assumption. AutoOrient update the orientation of the image to match the EXIF metadata properties and removes those properties from the metadata.

As you can see on your breakpoints. The input image has physical dimensions of 1536x2048 (portrait) but is displayed rotated 90degrees (landscape). Once processed the image pixels are 2048x1536 which matches the landscape display orientation of the input.

bertasoft commented 5 months ago

Ok, how can I have the rotated version of the image like my example?

Something that follow the exif infos and give me the jpg rotated, any c# code example?

Now i'm using something like this

private const int exifOrientationID = 0x112; //274

public static void ExifRotate(this Image img) { if (!img.PropertyIdList.Contains(exifOrientationID)) return;

var prop = img.GetPropertyItem(exifOrientationID);
int val = BitConverter.ToUInt16(prop.Value, 0);
var rot = RotateFlipType.RotateNoneFlipNone;

if (val == 3 || val == 4)
    rot = RotateFlipType.Rotate180FlipNone;
else if (val == 5 || val == 6)
    rot = RotateFlipType.Rotate90FlipNone;
else if (val == 7 || val == 8)
    rot = RotateFlipType.Rotate270FlipNone;

if (val == 2 || val == 4 || val == 5 || val == 7)
    rot |= RotateFlipType.RotateNoneFlipX;

if (rot != RotateFlipType.RotateNoneFlipNone)
{
    img.RotateFlip(rot);
    img.RemovePropertyItem(exifOrientationID);
}

}

Thanks for clarification,