简体   繁体   中英

How to make tkinter slideshow scale images to fit on screen?

I am trying to make a image slide show using Tkinter, but I have run into the problem that any image which is not the same as the native resolution of my monitor (or less) will get cut off.

My current code:

import tkinter as tk
from itertools import cycle
from PIL import ImageTk, Image
import random


with open('list.txt', 'r') as f: #sparces the list file into something the code can understand
    lines = f.read().strip('[]')
    images = [i.strip("\" ") for i in lines.split(',')]

var1 = input("random?")
if var1 == "yes" or var1 == "Yes":
    #runs the randomized version of the code
    random.shuffle(images)

    photos = cycle(ImageTk.PhotoImage(Image.open(image)) for image in images)

    def slideShow():
        img = next(photos)
        displayCanvas.config(image=img)
        root.after(1200, slideShow) 

    root = tk.Tk()
    root.overrideredirect(True)
    width = root.winfo_screenwidth()
    height = root.winfo_screenwidth()
    root.geometry('%dx%d' % (1920, 1080))
    displayCanvas = tk.Label(root)
    displayCanvas.pack()
    root.after(1000, lambda: slideShow())
    root.mainloop()
elif var1 == "no" or var1 == "No":
    #runs the alphabetical version of the code

    photos = cycle(ImageTk.PhotoImage(Image.open(image)) for image in images)

    def slideShow():
        img = next(photos)
        displayCanvas.config(image=img)
        root.after(1200, slideShow) 

    root = tk.Tk()
    root.overrideredirect(True)
    width = root.winfo_screenwidth()
    height = root.winfo_screenwidth()
    root.geometry('%dx%d' % (1920, 1080))
    displayCanvas = tk.Label(root)
    displayCanvas.pack()
    root.after(1000, lambda: slideShow())
    root.mainloop()
else:
    print("you gotta answer with yes,Yes,no,No,")
    exit

how can i make it so that images are scaled to properly show on screen?

You can use PIL Image.resize() function to resize the image. Below is a modified code base on yours:

with open('list.txt', 'r') as f: #sparces the list file into something the code can understand
  lines = f.read().strip('[]')
  images = [i.strip('"') for i in lines.split(',')]

var1 = input("Random ? ")
if var1.lower() in ("yes", "y"):
    random.shuffle(images)

root = tk.Tk()
root.attributes('-fullscreen', 1) # make the root window fullscreen
root.config(cursor="none")  # hide the mouse cursor

# get the screen size
scr_width = root.winfo_screenwidth()
scr_height = root.winfo_screenheight()

print("Preparing images ...")
photos = []
for image in images:
    img = Image.open(image)
    if img.width > scr_width or img.height > scr_height:
        # only resize image bigger than the screen
        ratio = min(scr_width/img.width, scr_height/img.height)
        img = img.resize((int(img.width*ratio), int(img.height*ratio)))
    photos.append(ImageTk.PhotoImage(img))

slides = cycle(photos)

def slideShow():
    displayCanvas.config(image=next(slides))
    root.after(1200, slideShow)

displayCanvas = tk.Label(root)
displayCanvas.pack(expand=1, fill=tk.BOTH)

root.bind('<Escape>', lambda e: root.destroy()) # allow Esc key to terminate the slide show
slideShow() # start the slide show

root.focus_force()
root.mainloop()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM