Python 实现一个简单的照片播放器,遍历播放一个目录及其子目录下所有的jpg

import cv2
import filetype
import os
import time
from tkinter import *
from tkinter.filedialog import askdirectory
import threading

exitFlag = 0
def select_path():
    path_ = askdirectory() #return file location
    if path_ == "":
        path.get()
    else:
        path_ = path_.replace("/", "\\")  #convert
        path.set(path_)

def open_path():
    dir = os.path.dirname(path.get()+"\\")
    os.system('start ' + dir)

def show_image(image_name):
    kind = filetype.guess(image_name)
    if kind is None or kind.extension != 'jpg':
        print("invalid type for " + image_name)
        return

    image = cv2.imread(image_name)

    # adjust size
    height, width = image.shape[:2]
    height_coefficient = height // 720
    width_coefficient = width // 1280
    zoom_coefficient = 0
    if height_coefficient > zoom_coefficient:
        zoom_coefficient = height_coefficient
    if width_coefficient > zoom_coefficient:
        zoom_coefficient = width_coefficient
    if zoom_coefficient > 0:
        image = cv2.resize(image, (int(width / zoom_coefficient), int(height / zoom_coefficient)),
                           interpolation=cv2.INTER_CUBIC)

    # show image
    #cv2.namedWindow(image_name, 0)
    #cv2.moveWindow(image_name, 0, 0)
    cv2.imshow(image_name, image)
    cv2.waitKey(3000)
    cv2.destroyWindow(image_name)

def loop_image():
    global exitFlag
    target_folder = path.get()
    image_number = 0
    for filepath, dirnames, filenames in os.walk(target_folder):
        for filename in filenames:
            if exitFlag:
                return
            print(filepath + '\\' + filename)
            show_image(filepath + '\\' + filename)
            image_number = image_number + 1
    #print(image_number)

def stop_showing():
    global exitFlag
    exitFlag = 1

def start_showing():
    global exitFlag
    exitFlag = 0
    thread1 = my_thread(1, "image_showing_thread")
    thread1.start()

def quit_process():
    os._exit(0)

class my_thread(threading.Thread):  # inherit threading.Thread
    def __init__(self, threadID, name):
        threading.Thread.__init__(self)
        self.threadID = threadID
        self.name = name

    def run(self):  # move specific ligic here
        print("Starting " + self.name)
        loop_image()
        print("Exiting " + self.name)

if __name__ == '__main__':
    root = Tk()
    root.title("Image scanner")
    path = StringVar()
    path.set(os.path.abspath("."))
    Label(root, text="target path:").grid(row=0, column=0)
    Entry(root, textvariable=path, state="readonly").grid(row=0, column=1, ipadx=50)
    Button(root, text="select path", command=select_path).grid(row=0, column=2)
    Button(root, text="start showing", command=start_showing).grid(row=0, column=3)
    Button(root, text="stop showing", command=stop_showing).grid(row=0, column=4)
    Button(root, text="exit process", command=quit_process).grid(row=0, column=5)

    root.mainloop()

每张照片播放3秒,从此解放双手看照片

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值