# Why does pytorch rotate a numpy image when converted to tensor

**URL:** <https://discuss.pytorch.org/t/why-does-pytorch-rotate-a-numpy-image-when-converted-to-tensor/155231>\
**Category:** vision\
**Created:** [June 28, 2022, 7:51am UTC](https://discuss.pytorch.org/t/why-does-pytorch-rotate-a-numpy-image-when-converted-to-tensor/155231 "2022-06-28T07:51:57Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![binbbaz](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/binbbaz/32/44730_2.png) [@binbbaz](https://discuss.pytorch.org/u/binbbaz)\
**Post date:** [June 28, 2022, 7:51am UTC](https://discuss.pytorch.org/t/why-does-pytorch-rotate-a-numpy-image-when-converted-to-tensor/155231/1 "2022-06-28T07:51:57Z")

</div>

I have a numpy image i tried converting to a tensor

![cats](https://discuss.pytorch.org/uploads/default/original/3X/c/5/c5a7d0544a6bdcc5e00ef8b97656a839b7b17f4b.jpeg)

`tensor_img = torch.from_numpy(numpy_img).permute(2, 1, 0).unsqueeze(0)`

I got a rotated and flipped image. What might the problem

![Screen Shot 2022-06-28 at 11.51.19 AM](https://discuss.pytorch.org/uploads/default/original/3X/e/a/eaac20f3356ce4f4a16a22f3d5ad45b2450b6870.png)

---

<div class="post-metadata">

**Author:** ![binbbaz](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/binbbaz/32/44730_2.png) [@binbbaz](https://discuss.pytorch.org/u/binbbaz)\
**Post date:** [June 28, 2022, 8:18am UTC](https://discuss.pytorch.org/t/why-does-pytorch-rotate-a-numpy-image-when-converted-to-tensor/155231/2 "2022-06-28T08:18:23Z")

</div>

So I figured it out in case someone falls into same problem. I had earlier splitted the original image into blocks where each block is stored in a list. This list is then converted into numpy and reshaped accordingly, resulting into the first image above.

`import torchvision.transforms as T`  
`transform = T.ToTensor()`  
`tensor_img = transform(numpy_img).unsqueeze(0)`

solved the problem

---

<div class="post-metadata">

**Author:** ![ptrblck](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/32/1823_2.png) [@ptrblck](https://discuss.pytorch.org/u/ptrblck)\
**Post date:** [June 28, 2022, 10:13am UTC](https://discuss.pytorch.org/t/why-does-pytorch-rotate-a-numpy-image-when-converted-to-tensor/155231/3 "2022-06-28T10:13:27Z")

</div>

While `ToTensor` should also work, I think the original issue is caused by a wrong permutation as most likely your numpy image has the shape `[H, W, C]` which you would like to transform to a tensor in `[C, H, W]`. To do so you would need to call `.permute(2, 0, 1)`.
