0

I want to rotate MxNx3 images (e.g. 640x480x3) images by 90 and 270 degrees using numpy functions like swapaxes, transpose etc and keep the image dimensions intact after rotation.

i.e. A 640x480x3 image should remain 640x480x3 after it is rotated by 90 degrees.

I have already implemented the rotation functionality thank to this link. It works fine for MxMx3 images

Below is my code.

for 90 deg CW rotation
rotated_img = np.transpose(image.swapaxes(-1, -0)[..., ::-1], (1,2,0))

for 270 deg CW rotation

rotated_img = np.transpose(image.swapaxes(-0,-1)[...,::-1,:], (1, 2, 0))

I want to know:

  1. how to rotate MxNX3 images by 90 and 270 degree and keep their dimensions intact?
  2. it is possible to achieve (1) by using only numpy swapaxes, transpose etc?
  3. is Affine transform the only way for rotating MxNx3 images by 90 and 270 degrees and keeping their dimensions intact?
Vishal
  • 21
  • 4
  • Rotating a rectangle by 90° without changing its size will perforce stretch the content (dilate one axis and contract the other). Dilation must create new pixels and contraction drop some. –  Dec 21 '21 at 12:59
  • A 90 deg rotation wouldn't preserve the dimensions of an image. If you want to preserve the dimensions, you have to do something non-trivial. An affine transformation is one such thing you can do. You are essentially losing some data along the axis that gets squished by doing so. – Sriram Dec 21 '21 at 13:01
  • @YvesDaoust if I understand you correctly then some sort of interpolation will be involved in rotating MxNx3 image to keep its dimensions intact. Actually scipy/pillow rotations do involve interpolation. And only way to keep dimensions intact would then be Affine Transform? – Vishal Dec 21 '21 at 13:38
  • You can use a scaling transform combined to rigid 90° rotation. Simpler than affine. –  Dec 21 '21 at 13:39

0 Answers0