Task: Image rotation

Write a python script/function, that rotates an image by a given angle. The solution should consist of the following steps:

  • load an image,
  • for a given rotation angle, compute the corners of the new image and the new image (rectangular) bounding-box,
  • preallocate the array for the new image,
  • iterate all pixels in the new image, fill-in their intensities/colors from the original image
  • pixel intensity look-up must use geometric transformation of pixel coordinates (the rotation)
  • the image should be rotated around its center, so image coordinates origin (0,0 is in the upper-left corner) must be properly handled

Some tips:

  • The transformed coordinates must be converted to integer numbers to be used as indices.
  • When looking-up the pixel value in the original image, the image borders must be checked and the lookup must be done only for coordinates inside
  • The new image can be pre-allocated as new_img = np.zeros( img.shape ). This works both for gray-scale (2D) and colour (3D) image.
  • The pixel assignment can be done (provided there are correct x1, y1, x2, y2) as new_img[y2,x2] = img[y1,x1]. Works for both gray-scale and RGB cases. In the first case, a single value is assigned, in the second case, the RGB vector is assigned.

Example results

In [2]:
%run task_rot