How can I rotate an image about a specific pixel in Python? I am trying to de-rotate a set of images of the night sky. Since the stars rotate around Polaris, I could define Polaris as the center of rotation and rotate each image to line up the stars.
With a little math: if each image's pixel position is vector a
, and position of Polaris is p
, then new position new_p
is
new_p = a + R * (a-p)
where R is Rotation matrix.
There will be problem, as new_p
is probably not an integer valued position-vector. You
can do it backwards. For each pixel of rotated image apply the inverse of above transform,
than you will get pixel from original image. As it could be not integer also, sample the neighbor pixels like in wu-pixels (the amount of dot spread around can be used as the sampling weight).
In phadej's answer the transformation between the old and new coordinates of a point on the image is an affine transformation.
PIL (Python Imaging Library) has an image method called transform
which can perform an affine transformation of an image.
The documentation for transform
is near the bottom of this page.