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秒,从此解放双手看照片