使用Python TKinter/imageTK在屏幕上的图像之间淡入淡出 [英] Fade between images on screen using Python TKinter / imageTK

查看:319
本文介绍了使用Python TKinter/imageTK在屏幕上的图像之间淡入淡出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是python新手,一直在制作一个有点奇怪的幻灯片脚本,该脚本在图像之间循环,并从另一个文件中获取变量以稳定"在图像上.

I am a python newbie and have been making a somewhat odd slideshow script that cycles through images and also sources a variable from another file to 'settle' on an image.

我确定我的代码很悲惨.但这确实有效(见下文)!

I'm sure my code is tragic. But it does work (see below)!

我的问题是-如何使它在图像之间淡化,而不是突然变成白色然后突然变成当前当前的下一个图像?我应该看一下转换模块吗?

My question is - how would I make it fade between images instead of the jerky go to white momentarily then to next image which it does currently? Is there a transitions module I should look at?

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

推荐答案

这可以使用blend函数来实现.

This can be achieved using the blend function.

 Image.blend(image1, image2, alpha) ⇒ image

通过在给定图像之间进行插值(使用恒定的alpha)来创建新图像.两张图片必须具有相同的尺寸和模式.

Creates a new image by interpolating between the given images, using a constant alpha. Both images must have the same size and mode.

out = image1 * (1.0 - alpha) + image2 * alpha

如果alpha为0.0,则返回第一张图像的副本.如果alpha为1.0,则返回第二张图像的副本.对alpha值没有限制.如有必要,结果会被裁剪以适合允许的输出范围.

If the alpha is 0.0, a copy of the first image is returned. If the alpha is 1.0, a copy of the second image is returned. There are no restrictions on the alpha value. If necessary, the result is clipped to fit into the allowed output range.

所以您可能会遇到这样的事情:

So you could have something like this:

alpha = 0
while 1.0 > alpha:
    image.blend(img1,img2,alpha)
    alpha = alpha + 0.01
    label_image.update()

一个例子在这里,还没有时间进行测试,但是您知道了-

An example is here, havn't had time to test this but you get the idea-

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

这篇关于使用Python TKinter/imageTK在屏幕上的图像之间淡入淡出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆