How to rotate an array of bits

I currently have a PIL Image object which I store as an array of bits (1’s and 0’s). However I now would like to be able to rotate that image 45 degrees. One way to do it is take the original PIL image, apply a transform matrix on it, then convert that to an array of bits. Problem with this approach is that it’s computationally expensive, especially if I want to start doing more than just one rotation. It would be faster to just modify the array of bits directly.

I tried using numpy.roll:

numpy.roll(bits, 45) # rotate 45 degrees

Unfortunately, this just does a circular shift, not an actual angular rotation.

What algorithm can I use on the array of bits to give me the desired output without having to go through the original image? Even though my application is in Python, your answer can be in whatever language you feel comfortable with, I’m more interested in the algorithm itself not the syntax 🙂

2

If you always want to rotate an image by a specific angle each time, you only need to do the heavy math once (and really, it’s not that heavy). The best way to do this is by working the process backwards. For every pixel in the destination image, you calculate the corresponding location in the starting image. This “source” pixel coordinate is saved in what is called a Lookup Table or LUT. Once you build this LUT for the angle and origin of location that you want, then it is very quick to use each entry of the LUT to “move” the pixels of a source image to the correct pixel of the destination image. Where this gets tricky and involves more math is whn deciding how to weight the source date to come up with the destination data. When you work backwards from the integer destination image coordinates, you will end up with floating point coordinates in the source image. These floating point coordinates need to be interpolated. The simplest method is to use the “Nearest Neighbor” integer pixel coordinate to the calculated floating point values, but there are other algorithms such as “Bi-Linear” or “Bi-Cubic” that will provide a better quality of the finished image (but are of course slower than Nearest NEighbor”

To see that it can’t be just a question of shifting points around, just draw a frame around your image: In the rotated version, the image will have diagonal edges and so won’t even fit in the dimensions of the original image.

So, to rotate it you’ll have to compute where each pixel goes. Note that the top left point (0,1) will not end up at (1,1) but at a point on the diagonal whose distance from the origin is 1: namely, at (sqrt(2)/2, sqrt(2)/2). Since sin(45) = cos(45) = sqrt(2)/2, the algorithm you’re looking for is multiplication of each point’s coordinates by this transformation matrix:

|  sqrt(2)/2  sqrt(2)/2 |
| -sqrt(2)/2  sqrt(2)/2 |

See the wikipedia page on Transformation Matrices for the details. So, you have to do a lot of multiplications but you don’t have to calculate any sines or cosines (I just did that for you from memory). You can take some other shortcuts, as the other answers suggested, but one way or another you have to compute where each pixel must go.

Of course you’ll also have to crop or pad to create a normal rectangular image.

PS. Better yet, instead of implementing this solution, outsource it to a graphics library that does rotations. Or check out this SO question and the solution suggested in the comments under the question:

Consider scipy.ndimage.interpolation.shift() and rotate() or skimage.transform.fast_homography() for interpolated translations and rotations of 2D numpy arrays.

there’s no way to do rotation by arbitrary angles without sines and cosines, but it is possible to optimize the calculation depending on what other constraints you’re prepared to impose. This can get very complicated, but can result in huge improvements for the “special case” you’ve defined.

If your images are all the same size and reasonably small, like your butterfly, and you were planning to do a lot of them, you could preform the slow calculation once, and simply remember the x,y of each pixel of the source image that became the x’,y’ of the destination image

2

If you only want to limit rotation to 45 degrees, you don’t need to use sin and cos, for that particular case I would advice you to use a fast isometric diamond drawing algorithm and adapt it to your matrix. Usually a couple nested loops and 4 variables (x, y, width, height) will do it.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật