如何显示图像,关闭并显示下一个图像

发布于 2025-02-03 01:06:32 字数 698 浏览 3 评论 0原文

我想为美国手语手柄制定培训计划。我为字母组的每个字母都有一个图像库,想一次通过一个字符读取脚本,然后显示一组毫秒数的手指下图像,然后转到下一个图像。 我是编程的新手,很喜欢以菜鸟为导向的答案,谢谢! 该脚本根据“ Awesome.txt”中的文本打开了正确的图像,但是我有一些我不知道如何解决的问题:

  1. 这些图像在Windows照片中打开,另一个在另一个上堆放。我需要它们在屏幕上闪烁以获取毫秒#的集合,并由下一个图像替换。
  2. 如果角色不是字母,我需要一个捕捉,因此它会跳过并继续前进。 这是我到目前为止写的代码:
from PIL import Image
delaytime=input("set speed in milliseconds")
def displayme(imagename):
    image = Image.open(imagename)
    image.show()
    time.sleep(int(delaytime)/1000)
    image.close()
file = open('awesome.txt', 'r')
while 1:
    # read by character
    char = file.read(1)
    imagename=char+".png"
    if not char:
        break
    displayme(imagename)
file.close()

I want to create a training program for American Sign Language fingerspelling. I have a library of images for each letter of the alphabet and would like to have the script read through a text file one character at a time and then display the fingerspelling image for a set number of milliseconds, then move on to the next image.
I am new to programming and would appreciate noob-oriented answers, thanks!
The script opens the correct images based on the text in “awesome.txt”, but I have a couple issues I don’t know how to resolve:

  1. The images are opening in Windows Photos and piling one on top of the other. I need them to flash on the screen for the set # of milliseconds and be replaced by the next image.
  2. I need a catch in case the character is not a letter, so it skips it and moves on to the next.
    This is the code I have written so far:
from PIL import Image
delaytime=input("set speed in milliseconds")
def displayme(imagename):
    image = Image.open(imagename)
    image.show()
    time.sleep(int(delaytime)/1000)
    image.close()
file = open('awesome.txt', 'r')
while 1:
    # read by character
    char = file.read(1)
    imagename=char+".png"
    if not char:
        break
    displayme(imagename)
file.close()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

机场等船 2025-02-10 01:06:32

这就是我想到的:

import tkinter
from PIL import ImageTk, Image


def load_images_from(file_path):
    # Returns list of image objects enumerated in file
    images = []
    # with is a whats called context manager which
    # in this case will automatically close opened file
    with open(file_path, "r") as file:
        for line in file.readlines():
            if line:
                images.append(
                    ImageTk.PhotoImage(
                        Image.open(f"{line.strip()}.png"),
                    )
                )
    return images


def main():
    # create new window object
    window = tkinter.Tk()
    # load images and keep index of currently displayed
    # whats interesting, tkinter seems not to properly
    # handle reference counting of images (it doesn't keep
    # them alive?) as without keeping images in array, they
    # doesn't seem to show.
    images = load_images_from("awesome.txt")
    index = 0
    # insert widgets into window frame
    label_component = tkinter.Label(window, image=images[index])
    label_component.pack()

    # clicking in window will result changing image
    def callback(event):
        nonlocal index
        index += 1
        # check index error, if it is pointing behind the array
        # it means we have already shown all of the images so we can exit
        if index >= len(images):
            window.destroy()
            return
        # change currently displayed image
        label_component.configure(image=images[index])

    # attach event listener which will call callback() every
    # time left mouse button is clicked
    window.bind("<1>", callback)
    # keeps script alive as program goes
    window.mainloop()


if __name__ == "__main__":
    main()

尝试 this 用于间隔调用回调函数

That's what I came up with:

import tkinter
from PIL import ImageTk, Image


def load_images_from(file_path):
    # Returns list of image objects enumerated in file
    images = []
    # with is a whats called context manager which
    # in this case will automatically close opened file
    with open(file_path, "r") as file:
        for line in file.readlines():
            if line:
                images.append(
                    ImageTk.PhotoImage(
                        Image.open(f"{line.strip()}.png"),
                    )
                )
    return images


def main():
    # create new window object
    window = tkinter.Tk()
    # load images and keep index of currently displayed
    # whats interesting, tkinter seems not to properly
    # handle reference counting of images (it doesn't keep
    # them alive?) as without keeping images in array, they
    # doesn't seem to show.
    images = load_images_from("awesome.txt")
    index = 0
    # insert widgets into window frame
    label_component = tkinter.Label(window, image=images[index])
    label_component.pack()

    # clicking in window will result changing image
    def callback(event):
        nonlocal index
        index += 1
        # check index error, if it is pointing behind the array
        # it means we have already shown all of the images so we can exit
        if index >= len(images):
            window.destroy()
            return
        # change currently displayed image
        label_component.configure(image=images[index])

    # attach event listener which will call callback() every
    # time left mouse button is clicked
    window.bind("<1>", callback)
    # keeps script alive as program goes
    window.mainloop()


if __name__ == "__main__":
    main()

Try this for calling callback function in interval

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文