Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/352.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Python TKinter/imageTK在屏幕上的图像之间淡入淡出_Python_Tkinter_Python Imaging Library - Fatal编程技术网

使用Python TKinter/imageTK在屏幕上的图像之间淡入淡出

使用Python TKinter/imageTK在屏幕上的图像之间淡入淡出,python,tkinter,python-imaging-library,Python,Tkinter,Python Imaging Library,我是一个python新手,一直在制作一个有点奇怪的幻灯片脚本,它可以循环浏览图像,还可以从另一个文件中获取一个变量来“确定”图像 我确信我的代码是悲剧。但它确实有效(见下文) 我的问题是-我如何使它在两幅图像之间淡出,而不是突然变为白色,然后进入下一幅图像,它目前正在这样做?我应该看一看过渡模块吗 from Tkinter import * import Image, ImageTk, random, string class MyApp(Tk): def __init__(self):

我是一个python新手,一直在制作一个有点奇怪的幻灯片脚本,它可以循环浏览图像,还可以从另一个文件中获取一个变量来“确定”图像

我确信我的代码是悲剧。但它确实有效(见下文)

我的问题是-我如何使它在两幅图像之间淡出,而不是突然变为白色,然后进入下一幅图像,它目前正在这样做?我应该看一看过渡模块吗

from Tkinter import *
import Image, ImageTk, random, string

class MyApp(Tk):

def __init__(self):
    Tk.__init__(self)
    fr = Frame(self)
    fr.pack()
    self.canvas  = Canvas(fr, height = 400, width = 600)
    self.canvas.pack()

    self.old_label_image = None
    self.position = 0
    self.command = 0
    self.oldcommand = 0

    self.slideshow()
    self.debug()

def debug(self):
    self.QUIT = Button(self)
    self.QUIT["text"] = "QUIT!" + str(self.command)
    self.QUIT["fg"]   = "red"
    self.QUIT["command"] =  self.quit

    self.QUIT.pack({"side": "right"})

def slideshow (self):

    if self.command != self.oldcommand:
        self.after_cancel(self.huh)
        # run through random between 2-5 changes 
        # then settle on command for 30 seconds
        self.title("Title: PAUSE")
        self.oldcommand = self.command
        self.slideshow()
    else:
        file = str(self.position) + '.jpg'
        image1 = Image.open(file)
        self.tkpi = ImageTk.PhotoImage(image1)
        label_image = Label(self, image=self.tkpi)
        label_image.place(x=0,y=0,width=image1.size[0],height=image1.size[1])
        self.title("Title: " + file)

        if self.old_label_image is not None:
            self.old_label_image.destroy()
        self.old_label_image = label_image

        # make this random instead of pregressional
        if self.position is not 1:
            self.position = self.position + 1
        else:
            self.position = 0

        commandfile = open('command.txt', 'r')
        self.command = string.atoi(commandfile.readline())
        commandfile.close()

        int = random.randint(2000, 5000)
        self.huh = self.after(int, self.slideshow)
        #self.after_cancel(huh) - works ! so maybe can do from below Fn?

if __name__ == "__main__":
root = MyApp()
root.mainloop()

这可以通过使用混合功能来实现

 Image.blend(image1, image2, alpha) ⇒ image
通过使用常量alpha在给定图像之间插值来创建新图像。两个图像必须具有相同的大小和模式

out = image1 * (1.0 - alpha) + image2 * alpha
如果alpha为0.0,则返回第一个图像的副本。如果alpha为1.0,则返回第二个图像的副本。alpha值没有限制。如有必要,将剪裁结果以适合允许的输出范围

所以你可以有这样的东西:

alpha = 0
while 1.0 > alpha:
    image.blend(img1,img2,alpha)
    alpha = alpha + 0.01
    label_image.update()
这里有一个例子,没有时间来测试,但你知道了-

from PIL import image
import time
white = image.open("white_248x.jpg")
black = image.open("black_248x.jpg")
new_img = image.open("white_248x.jpg")
root = Tk()
image_label = label(root, image=new_img)
image_label.pack()
alpha = 0
while 1.0 > alpha:
    new_img = image.blend(white,black,alpha)
    alpha = alpha + 0.01
    time.sleep(0.1)
    image_label.update()
root.mainloop()

你能提供“command.txt”吗?没有它,我无法测试你的代码。我试图删除它,但我只看到蓝色背景,而不是我的jpg图像。谢谢-这是一个非常及时的时刻:-)你有一个脚本这样做的例子吗?它在底部的原始问题中-我已经编辑了它:)我在你的例子中得到以下错误(对于声明标签的行),在Python 3.x中:
\u tkinter.TclError:image“”不存在
(图像文件确实存在,并且声明了它的变量)。