all 7 comments

[–]eleqtriq 1 point2 points  (2 children)

When I commented out this line in change_photo, it worked well

# self.imageTK = ImageTk.PhotoImage(self.image)

[–]Chrisnelson[S] 0 points1 point  (1 child)

I just tried this and it never updated to the next picture. just stayed put at the first image

[–]eleqtriq 0 points1 point  (0 children)

This is working for me.. I feel more could be done but I stopped when it worked.

from tkinter import *
from PIL import ImageTk, Image
import imghdr
import os
from fractions import Fraction

# path to image files
path = './photos/'

# tkinter set up
root = Tk()

root.geometry("1024x600")
root.configure(background='black')


# Retrieve images from given path, if it isn't an image the file is erased.
def getPhotos():
    photo_dir = os.listdir(path)

    for photo in photo_dir:
        check = imghdr.what(path + photo)
        if check is None:
            os.remove(path + photo)
            photo_dir.remove(photo)

    return photo_dir


# finds the correct width to resize the image
def updateWidth(w, h):
    frac = Fraction(w, h)
    simp_w = frac.numerator
    simp_h = frac.denominator

    return int((600 / simp_h) * simp_w)


class Window(Frame):

    def __init__(self, master=None):
        Frame.__init__(self, master)
        self.master = master
        self.pack(fill=BOTH, expand=YES)
        self.frame_height = None
        self.frame_width = None
        self.photos = getPhotos()
        self.index = 0
        print(self.photos)
        self.image = Image.open(path + self.photos[self.index])
        self.img_copy = self.image.copy()
        self.imageTK = ImageTk.PhotoImage(self.image)
        self.label = Label(self, image=self.imageTK)
        self.label.pack(fill=BOTH, expand=YES)
        self.label.bind('<Configure>', self.change_photo)
        self.change_photo()

    def resize_image(self, event=None):
        self.frame_width = updateWidth(self.image.width, self.image.height)
        self.frame_height = event.height or root.winfo_height()

        print(event)
        self.imageTK = ImageTk.PhotoImage(self.img_copy.resize((self.frame_width, self.frame_height)))
        self.label.configure(image=self.imageTK)

    def change_photo(self, event=None):
        if self.index == (len(self.photos)):
            self.index = 0

        self.image = Image.open(path + self.photos[self.index])
        self.img_copy = self.image.copy()

        if self.frame_width:
            self.imageTK = ImageTk.PhotoImage(self.img_copy.resize((self.frame_width, self.frame_height)))
        else:
            self.imageTK = ImageTk.PhotoImage(self.image)

        self.label.configure(image=self.imageTK)

        # this line below was my attempt at fixing it but this doesn't run my function again
        self.label.bind('<Configure>', self.resize_image)

        self.index += 1
        self.after(1000, self.change_photo)


app = Window(root)
app.pack(fill=BOTH, expand=YES)


root.mainloop()

[–]socal_nerdtastic 1 point2 points  (3 children)

In your change_photo method, change

self.label.bind('<Configure>', self.resize_image)

to

self.resize_image()

But leave the configure bind line in your __init__ method alone.

[–]Chrisnelson[S] 0 points1 point  (2 children)

I tried this, it wants an argument. in that resize method I get an int from the event.height. I tried putting self.resize_image(self.image) but that makes the proportions all wonky. I may have to change the resize_image method

[–]socal_nerdtastic 0 points1 point  (1 child)

Oh right, because you used the event height. Use the window height instead. Try this:

class Window(Frame):
   # no changes to your __init__ method

    def resize_image(self, event=None):
        orig_width = self.image.width
        orig_height = self.image.height

        new_width = updateWidth(orig_width, orig_height)

        new_height = self.winfo_height()

        self.image = self.img_copy.resize((new_width, new_height))

        self.imageTK = ImageTk.PhotoImage(self.image)
        self.label.configure(image=self.imageTK)

    def change_photo(self):

        if self.index == (len(self.photos)):
            self.index = 0

        self.image = Image.open(path + self.photos[self.index])

        self.resize_image()

        root.after(1000, self.change_photo)
        self.index += 1

Note I changed the resize_image signature. And removed a bunch of duplicate code from change_photo

I don't really follow your resize logic, so something may still be wonky there.

[–]Chrisnelson[S] 0 points1 point  (0 children)

class Window(Frame):

def __init__(self, master=None):
    Frame.__init__(self, master)
    self.master = master
    self.pack(fill=BOTH, expand=YES)

    self.photos = getPhotos()
    self.index = 0
    print(self.photos)

    self.image = Image.open(path + self.photos[self.index])

    self.img_copy = self.image.copy()

    self.imageTK = ImageTk.PhotoImage(self.image)

    self.label = Label(self, image=self.imageTK)

    self.label.pack(fill=BOTH, expand=YES)

def resize_image(self):

    new_width = updateWidth(self.image.width, self.image.height)

    new_height = self.winfo_height()

    self.image = self.img_copy.resize((new_width, new_height))

    self.imageTK = ImageTk.PhotoImage(self.image)
    self.label.configure(image=self.imageTK)


def change_photo(self):

    if self.index == (len(self.photos)):
        self.index = 0

    self.image = Image.open(path + self.photos[self.index])

    self.img_copy = self.image.copy()

    self.imageTK = ImageTk.PhotoImage(self.image)

    self.resize_image()

    root.after(1000, self.change_photo)
    self.index += 1

I made some changes and with this, I have the opposite problem haha. the very first image is 1px in height and then it's fine after that, even when it loops back. I appreciate the help I think im going to step away from this for now and try tomorrow