Textures loaded via UnityWebRequestTexture are sometimes rotated 90 degrees

Hello,

Our application downloads textures to the filesystem/persistent data (via DownloadHandlerFile) and then later displays them in a RawImage component by loading them from there like so:

UnityWebRequest request = UnityWebRequestTexture.GetTexture("file://" + localfilePath));

request.SendWebRequest().completed += (AsyncOperation op) => {
    if(!request.isFaulty())
    {
        Texture2D texture = DownloadHandlerTexture.GetContent(request);
        Debug.Log(texture.width + " / " + texture.height);
    }
};

This works just fine, except on some phones / in some cases the pictures are rotated after they’re loaded. What I mean by that is that the width and height are inverted, and the UI displays the image as rotated 90 ccw or cw.

Attached below is an image that does this:

  • It has a width of 1844 pixels
  • And a height of 4000 pixels
  • After loading, the created texture has a width of 4000 and a height of 1844

What may be important to note is that he photo is taken with a smartphone camera. I thought, perhaps theres something in the EXIF giving it an alternate rotation (I have no idea :P), but I can’t find anything strange in there.

Is there something really obvious I’m missing?

On further inspection, this also happens when loading above file using Unity - Scripting API: Texture2D.LoadImage

It doesn’t seem related to the UnityWebRequest implementation

This is actually probably the case. To avoid decompressing and recompressing the image, certain platforms actually just store some extra rotational information in the image (not really sure where) in order to do the final rotation for display. The actual image data is not rotated. Google might be of help here, but I doubt anything in Unity will know specifically about a rotate marker in some random platform’s file metadata. I’m not sure if that metadata would even be served to your program, unless it was embedded in the image itself.

Thanks! Perhaps I misunderstand, but if the actual image data is not rated (it doesn’t appear to be, opening the file on my PC displays the image in the correct orientation) and the metadata is not exposed to Unity, then what is causing it to flip the texture?

Think of it more as, “what is not causing it to unflip.”

I would speculate the image data is actually rotated but viewed on PC /Mac, the data gets un-rotated properly.

1 Like

You were correct, used this tool to display complete exif:

http://exif.regex.info/

And it had a rotation applied to it. Googled and found this:

Which points to this:

Managed to apply it to parse the image EXIF, check the rotation, and then rotate the texture using this:

Hope it helps someone

6 Likes

@nilsdr You are an absolute lifesaver! been struggling with this for a couple days now and thanks to you have resolved my issue as well.

Cheers

1 Like

@nilsdr Thank you so much, you save my life.

1 Like