Python Tkinter rotate image animation

2019-02-11 06:32发布

I have a PhotoImage that I created using PIL and then added to a TKinter canvas. The image shows up fine. However, I can't get the PIL rotate function to work correctly. Here is my code:

 #This works fine
 image = Image.open('img.png')
 canvas_image = ImageTk.PhotoImage(rotated_image)
 canvas_object = canvas.create_image(30+10*int(steps),250, image=canvas_image)
 canvas.pack()

 #this does not work
 canvas.delete(canvas_object)
 rotated_image = image.rotate(1)
 canvas_image = ImageTk.PhotoImage(rotated_image)
 canvas_object = canvas.create_image(30+10*int(steps),250, image=canvas_image)
 canvas.update()

However, the image just does not show up in this case. I want to be able to animate the rotation of an image, but I can't even get a rotation to work at all! Any suggestions are greatly appreciated.

Edit: correction, the rotation will not work after it's already been added to the screen

2条回答
欢心
2楼-- · 2019-02-11 07:03

Here is an example which rotates an image using Python3:

import tkinter as tk
from PIL import ImageTk
from PIL import Image

class SimpleApp(object):
    def __init__(self, master, filename, **kwargs):
        self.master = master
        self.filename = filename
        self.canvas = tk.Canvas(master, width=500, height=500)
        self.canvas.pack()

        self.update = self.draw().__next__
        master.after(100, self.update)

    def draw(self):
        image = Image.open(self.filename)
        angle = 0
        while True:
            tkimage = ImageTk.PhotoImage(image.rotate(angle))
            canvas_obj = self.canvas.create_image(
                250, 250, image=tkimage)
            self.master.after_idle(self.update)
            yield
            self.canvas.delete(canvas_obj)
            angle += 10
            angle %= 360

root = tk.Tk()
app = SimpleApp(root, '/path/to/image.png')
root.mainloop()

For a Python2 version, see the revision history.

查看更多
一夜七次
3楼-- · 2019-02-11 07:13

Here's unutbu's answer above, but in Python 3:

import time
import tkinter
from PIL import Image, ImageTk

class SimpleApp(object):
    def __init__(self, master, filename, **kwargs):
        self.master = master
        self.filename = filename
        self.canvas = tkinter.Canvas(master, width=500, height=500)
        self.canvas.pack()

        self.process_next_frame = self.draw().__next__  # Using "next(self.draw())" doesn't work
        master.after(1, self.process_next_frame)

    def draw(self):
        image = Image.open(self.filename)
        angle = 0
        print(self.process_next_frame)
        while True:
            tkimage = ImageTk.PhotoImage(image.rotate(angle))
            canvas_obj = self.canvas.create_image(250, 250, image=tkimage)
            self.master.after_idle(self.process_next_frame)
            yield
            self.canvas.delete(canvas_obj)
            angle += 1
            angle %= 360
            time.sleep(0.002)

root = tkinter.Tk()
app = SimpleApp(root, 'images.png')
root.mainloop()

It took me a while to get this working, so I figured I would share.

查看更多
登录 后发表回答