I would like to rotate an image to follow my mouse and my school computers don't have PIL.
2 Answers
Bryan's answer is technically correct in that the PhotoImage class doesn't contain rotation methods, nor does tk.Canvas. But that doesn't mean we can't fix that.
def copy_img(img):
newimg = tk.PhotoImage(width=img.width(), height=img.height())
for x in range(img.width()):
for y in range(img.height()):
rgb = '#%02x%02x%02x' % img.get(x, y)
newimg.put(rgb, (x, y))
return newimg
The above function creates a blank PhotoImage object, then fills each pixel of a new PhotoImage with data from the image passed into it, making a perfect copy, pixel by pixel... Which is a useless thing to do.
But! Let's say you wanted a copy of the image that was upside-down. Change the last line in the function to:
newimg.put(rgb, (x, img.height()-1 - y))
And voila! The function still reads from the top down, but it writes from the bottom up, resulting in an image mirrored on the y axis. Want it rotated 90-degrees to the right, instead?:
newimg.put(rgb, (img.height() - y, x))
Substituting the y for the x makes it write columns for rows, effectively rotating it.
How deep you go into image processing PhotoImage objects is up to you. If you can get access to PIL (Python Imaging Library)... someone has basically already done this work, refined it to be optimal for speed and memory consumption, and packaged it into convenient classes and functions for you. But if you can't or don't want PIL, you absolutely CAN rotate PhotoImage's. You'll just have to write the methods yourself.
Thanks to acw1668's post that hipped me to the basics of PhotoImage manipulation here: https://stackoverflow.com/a/41254261/9710971

- 161
- 1
- 7
You can't. The canvas doesn't support the ability to rotate images, and neither does the built-in PhotoImage class.
From the official Canvas documentation:
Individual items may be moved or scaled using widget commands described below, but they may not be rotated.

- 370,779
- 53
- 539
- 685
-
Why? You would think that would be a pretty basic thing to add and something that would be commonly used – Feesih0ps Sep 18 '17 at 20:47
-
@Feesih0ps: probably because it's been this way for 25 years and there hasn't been much of an uproar. Plus, unfortunately, the number of people who have actually worked on the Canvas implementation in the last decade is likely to be very close to one, if not less than that. – Bryan Oakley Sep 18 '17 at 22:07
-
I shouldn't even be using tkinter, to be honest, I wish my school could get around to installing pygame. Is there a way, with the base python 3.5 install, to work around this? Like manipulating gimp or paint into doing it from inside python? (apart from saving the image at 360 different rotations and importing them) – Feesih0ps Sep 19 '17 at 19:38
-
Its true that you can't rotate an image, BUT you can make a rotated copy of the image. I have a video game class I wrote doing animation that way. Here is a link to animation done that way on a very similar question I posted an answer to: https://stackoverflow.com/a/41254261/9710971. – garydavenport73 Aug 24 '21 at 03:00