C4 Upload my own images but get rotated

In several assignments where there is an optional section to upload and play around with my own images/photos, I upload them but they get rotated to the right for some reason as inputs and I don’t know if this messes up the results. If I open them by clicking on the images at the workspace (File->Open…) they are shown fine without rotation.
For example at C4 W4 A2:

content_image = np.array(Image.open("images/my_image.jpg").resize((img_size, img_size)))
content_image = tf.constant(np.reshape(content_image, ((1,) + content_image.shape)))

The plt.show() outputs my image rotated to the right for some reason. Any ideas why this happens? Does this make the results worse compared to the results if the image was not rotated?

Hello @Charalampos_Inglezos,

I can just make a guess - maybe it has something to do with this - that your image file stores information about the correct orientation but that Python code doesn’t use it. Guesses are guesses.

Whether or not it makes the result worse is your judgement - it is art anyway.

However, if you would like to rotate it back and you are willing to spend time on investigating the use of this tf.image.rot90 function, then give it a try.


PS: I intentionally rotated the image (with the function above) and ran through the code, and my cells responsible for artistic judgement tell me that it is fine (those cells are being surrounded by many cells for scientific judgement) :crazy_face:.

I have this problem not only in Neural Style Transfer but for example also in hand sign digits recognition. I took a picture of my hand and it got rotated and the network never got any sign right, not even once, and I tried both 1,2,3,4,5 fingers with 5 different photos of my hand but it always got it wrong. I don’t know if this was due to the rotation or it was just a bad distribution of data, different than the ones the net was trained on and thus it was normal to have such bad performance.

So if I remove that metadata info, the python code will get it right, not-rotated?

Hello @Charalampos_Inglezos,

There is no way I can tell. I don’t have your camera. I don’t actually know the reason. I was just guessing in my last reply.

@Charalampos_Inglezos, why don’t you just try your idea, and even if removing that metadata info didn’t help or even if the attempt to remove that metadata was failed, you would have learnt something new in the process.

You see from my previous post, rotating the image was what I tried if I had wanted to know what would happen to a rotated photo. Finding that link for you was what I did because I had wanted to see what might cause the situation you were encountering.

It is your endeavour, and just do whatever you wanted to for the situation you wanted to make a difference. :wink:

Good luck!


Can you try uploading a photo taken from your own phone to see if it also gets rotated or not? Could be just me, something I might be doing wrong.
Yes for the style transfer probably it doesn’t matter, but perhaps for the hand digits recognition plays a more significant role.

Hello @Charalampos_Inglezos,

I am not quite interested in that :wink:



No worries, thank you for the guidelines!
I think I’ve found the solution, we have to use ImageOps.exif_transpose()

from PIL import Image, ImageOps
content_image = Image.open("images/myimg.jpg")
content_image = ImageOps.exif_transpose(content_image) # remove exif orientation flag and keep original rotation
content_image = np.array(content_image.resize((img_size, img_size)))

Thank you @Charalampos_Inglezos for sharing!