gpt4 book ai didi

Python Tkinter 旋转图像动画

转载 作者:太空宇宙 更新时间:2023-11-03 12:40:47 27 4
gpt4 key购买 nike

我有一个使用 PIL 创建的 PhotoImage,然后将其添加到 TKinter Canvas 中。图像显示正常。但是,我无法使 PIL 旋转功能正常工作。这是我的代码:

 #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()

但是,在这种情况下图像不会显示。我希望能够为图像的旋转设置动画,但我什至无法让旋转工作!非常感谢任何建议。

编辑:更正,旋转已经添加到屏幕后将不起作用

最佳答案

这是一个使用 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()

对于 Python2 版本,请参阅 revision history .

关于Python Tkinter 旋转图像动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15736530/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com