1

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()
2
  • 1
    What you need is manual window handling, take a look at Tkinter -> docs.python.org/3/library/tkinter.html to create, open and close a window in which you can show an image. Pillow supports conversion to images understandable for tkinter. To check if char is a letter use char.isalpha(). May 30, 2022 at 16:43
  • @Christopher I think i can figure out what to do with "char.isalpha()", but I am don't really understand how to go about the manual window handling. if you or anyone can show me how you would adjust my code, that would be awesome. If that's too much of an ask, I'll understand and keep trying to find the answer! Thanks. May 30, 2022 at 17:40

1 Answer 1

0

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

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.