views:

45

answers:

2

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.

A: 

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).

phadej
I'm sure there is some library to do it for you, but it's nice programming exercice. For example `imagemagick` has this kind of "distortion" built-in: http://www.imagemagick.org/script/command-line-options.php?ImageMagick=gcvcqder3prdmhc6d8m6gk38v4#distort
phadej
+2  A: 

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.

PreludeAndFugue