简体   繁体   English

Tkinter - 同时使用滑块旋转图像

[英]Tkinter - rotate image with slider simultaneously

UPDATED -更新 -

New to tkinter tkinter 新手

Is it possible to rotate a picture by using a slider simultaneously.是否可以同时使用滑块来旋转图片。

I have an image of a rotatory dial, beneath this image is a slider listed from 0 to 360. I would like the image to rotate clockwise as the slider is moved from 0 to 360, and anticlockwise as the slider is returned from 360 to 0.我有一个旋转表盘的图像,该图像下方是一个从 0 到 360 列出的滑块。当滑块从 0 移动到 360 时,我希望图像顺时针旋转,当滑块从 360 返回到 0 时逆时针旋转.

ROTATION OF IMAGE WITH SLIDER WORKS CORRECTLY使用滑块正确旋转图像

I have ran into a bug, the image is black.我遇到了一个错误,图像是黑色的。 Perhaps the image is too zoomed in?也许图像被放大了? Apologies, I am new to python and tkinter.抱歉,我是 python 和 tkinter 的新手。

Here is how the GUI should look Correct GUI这是 GUI 的外观正确的 GUI

THIS IS HOW THE GUI LOOKS NOW Incorrect GUI with Slider这就是 GUI 现在的样子带有滑块的GUI不正确

THIS IS HOW THE GUI LOOKS REMOVING THUMBNAIL LINE THUMBNAIL这就是 GUI 移除缩略图线缩略图的样子

Here is the updated code这是更新的代码

# import necessary modules
from tkinter import * 
from tkinter import ttk
from PIL import Image, ImageTk

 
root = Tk()
root.title("Gesture Detection Application")
root.geometry("400x320") # set starting size of window
root.maxsize(400, 320) # width x height
root.config(bg="#6FAFE7") # set background color of root window
 
Heading = Label(root, text="Demonstrator Application2", bg="#2176C1", fg='white', relief=RAISED)
Heading.pack(ipady=5, fill='x')
Heading.config(font=("Font", 20)) # change font and size of label
   

image = Image.open("rotate_dial.png")
width, height = image.size
image.thumbnail((width/5, height/5))
photoimage = ImageTk.PhotoImage(image)
image_label = Label(root, image=photoimage, bg="white", relief=SUNKEN)
image_label.image = photoimage
image_label.pack(pady=5)

def rotate_image(degrees):
    new_image = image.rotate(-int(degrees))
    photoimage = ImageTk.PhotoImage(new_image)
    image_label.image = photoimage #Prevent garbage collection
    image_label.config(image = photoimage)
    

w2 = Scale(root, from_=0, to=360, tickinterval= 30, orient=HORIZONTAL, length=300, command = rotate_image)
w2.pack()
w2.set(0)
                                                                                            
root.mainloop()

You can rotate the image using PIL (which you've already imported).您可以使用 PIL(您已经导入)旋转图像。 You can link it to the Scale by adding a command.您可以通过添加命令将其链接到 Scale。

image = Image.open("rotate_dial.png")
width, height = image.size
image.thumbnail((width/5, height/5))
photoimage = ImageTk.PhotoImage(image)
image_label = Label(root, image=photoimage, bg="white", relief=SUNKEN)
image_label.pack(pady=5)

def rotate_image(degrees):
    new_image = image.rotate(-int(degrees))
    photoimage = ImageTk.PhotoImage(new_image)
    image_label.image = photoimage #Prevent garbage collection
    image_label.config(image = photoimage)
    

w2 = Scale(root, from_=0, to=360, tickinterval= 30, orient=HORIZONTAL, length=300, command = rotate_image)

Instead of creating a PhotoImage initially, it now creates a PIL Image object.它不是最初创建PhotoImage ,而是创建 PIL Image对象。 It then uses the height and width and the thumbnail function to replace the subsample .然后它使用高度和宽度以及thumbnail功能来替换subsample Then it uses ImageTk to turn it into a tkinter PhotoImage which is shown in the label.然后它使用ImageTk将其转换为标签中显示的PhotoImage The Scale now has a command, rotate_image , which recieves the scale value, which is the number of degrees, and then uses PIL to create a new rotated image, which is then turned into a PhotoImage and displayed in the label by changing it's image with .config . Scale 现在有一个命令rotate_image ,它接收缩放值,即度数,然后使用 PIL 创建一个新的旋转图像,然后将其转换为PhotoImage并通过更改其图像显示在标签中.config Now when you move the slider, the image rotates with it.现在,当您移动滑块时,图像会随之旋转。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM