【脚本库】第二显示屏上循环播放文件夹里的图片

在第二显示屏上循环播放文件夹里的图片

import os
import glob
from PIL import Image, ImageTk
import tkinter as tk
from screeninfo import get_monitors

def get_secondary_monitor_resolution():
    monitors = get_monitors()
    if len(monitors) >= 2:
        return monitors[1].width, monitors[1].height
    else:
        return None

def get_first_monitor_resolution():
    monitors = get_monitors()
    if len(monitors) >= 2:
        return monitors[0].width, monitors[0].height
    else:
        return None

def slideshow(window, image_files, delay):
    """幻灯片展示函数"""
    if not image_files:
        return

    current_image = 0
    num_images = len(image_files)

    # 创建标签用于显示图片
    image_label = tk.Label(window)
    image_label.pack()

    def show_image():
        """显示下一张图片"""
        nonlocal current_image

        # 打开并调整图片大小
        # image = Image.open(image_files[current_image])
        # image = image.resize((3840, 2160), Image.ANTIALIAS)
        # 获取显示器的分辨率

        # 获取第二个显示器的分辨率
        secondary_width, secondary_height = get_secondary_monitor_resolution()

        # 打开并调整图片大小
        image = Image.open(image_files[current_image])
        image.thumbnail((secondary_width, secondary_height), Image.ANTIALIAS)

        # 使用Pillow库将图片转换为Tkinter可用的格式
        tk_image = ImageTk.PhotoImage(image)

        # 更新标签显示图片
        image_label.configure(image=tk_image)
        image_label.image = tk_image

        # 更新当前图片索引
        current_image = (current_image + 1) % num_images

        # 设置下一次显示图片的延迟
        window.after(delay, show_image)

    # 启动幻灯片展示
    show_image()



def main():

    # 获取第二个显示器的分辨率
    first_width, first_height = get_first_monitor_resolution()



    # 指定图片所在的文件夹路径
    image_folder = r"D:\00 important\04 Projects\02 PTP\black"

    # 获取文件夹中的所有图片文件
    image_files = glob.glob(os.path.join(image_folder, "*.jpg")) + glob.glob(os.path.join(image_folder, "*.png"))

    # 创建Tkinter窗口
    window = tk.Tk()

    # 设置窗口标题
    window.title("Image Slideshow")

    # # 设置窗口大小
    # window.geometry("1920x1080")


    if first_width and first_height:
        # 设置窗口大小为第二个显示器的分辨率
        window.geometry("+{x}+{y}".format(x=first_width-2, y=-2))

    # # 设置窗口在扩展显示器上的位置
    # window.geometry("+{x}+{y}".format(x=2160, y=0))


    # 隐藏窗口边框和标题栏
    window.overrideredirect(True)

    # 设置窗口为全屏模式
    # window.attributes("-fullscreen", True)

    # 设置切换图片的延迟时间(以毫秒为单位)
    delay = 1000  # 1秒

    # 启动幻灯片展示
    slideshow(window, image_files, delay)

    # 运行窗口主循环
    window.mainloop()

if __name__ == "__main__":
    main()

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值