使用GUI在python中播放mp4视频

时间:2019-09-13 14:53:29

标签: python video tkinter

我想创建一个简单的照相亭。我用mp4文件作为动画。
一切都必须通过GUI中的“开始”按钮开始,该按钮将为用户运行一个循环。按下ESC键即可退出GUI。

在“用户”循环中,将有一个开始动画,如mp4视频-在omx播放器中,反复播放直到用户触摸触摸屏。我已经用监听器move_mouse做到了这一点,以杀死该进程。

然后我有另一个带有倒计时的动画,之后,相机拍摄了一张照片,然后在窗口中的窗口中显示照片,并带有重复或打印两个按钮。

监听器的问题在于,它会在捕获照片后冻结应用程序

我该如何解决这个问题?

try:
    from Tkinter import * # Python2
except ImportError:
    import tkinter as Tk  # Python3import Tkinter as tk

import sys
from time import sleep
from PIL import  Image, ImageTk
from datetime import datetime
from sh import gphoto2 as gp
import signal, os, subprocess, glob
from pynput import mouse
from pynput import keyboard

shot_date = datetime.now() .strftime("%Y-%m-%d")
shot_time = datetime.now() .strftime("%Y-%m-%d %H:%M:%S")
picID = "PIShots"
folder_name = shot_date + picID
save_location = "/home/pi/Desktop/gphoto/images/" + folder_name


# listener
def on_move(x,y):
    print("mouse move")
    playerOff()
    #pynput.mouse.Listener.stop
    playerOn()
    capturePhoto()

#  press key listener   
#def on_release(key):
#    print('{0} released'.format(
#        key))
#    if key == keyboard.Key.esc:
        # Stop listener
#        return False

#StartAnmation and listener
def Start():
    omxc= subprocess.Popen(['omxplayer','-b','--loop','--no-osd',"/home/pi/Desktop/START.mp4"])
    with mouse.Listener(
            on_move=on_move) as listener:
            listener.join()     

# get the latest file
def get_latest_file(path, *paths):
    """Returns the name of the latest (most recent) file
    of the joined path(s)"""
    fullpath = os.path.join(path, *paths)
    files = glob.glob(fullpath)  # You may use iglob in Python3
    if not files:                # I prefer using the negation
        return None                      # because it behaves like a shortcut
    latest_file = max(files, key=os.path.getctime)
    _, filename = os.path.split(latest_file)
    return filename

#start_animation in OMX player
def playerOn():
    omxc= subprocess.Popen(['omxplayer','-b',"/home/pi/Desktop/animacja.mp4"])

#Player Off
def playerOff():
    os.system('killall omxplayer.bin')

#CreateSaveFolder
def createSaveFolder():
    try:
        os.makedirs(save_location)
    except:
        print("Failed to create the new directory")
    os.chdir(save_location)

def quit(root):
    root.destroy()

def capturePhoto():
    status = 0
    createSaveFolder()
    sleep(6)
    os.system('fswebcam -r 1920x1080 -s brightness=70% -s gain=50% -S 10 --set lights=off --no-banner %H%M%S.jpg')
    print save_location
    location=get_latest_file(save_location,'*.jpg')
   #print location
    sciezkaZdj= save_location + "/" + location
    print sciezkaZdj
    im = Image.open(sciezkaZdj)
    width, height =im.size

    LEFT_HALF = 200, 0, width-400 ,height
    im = im.crop(LEFT_HALF)
    im = im.transpose(Image.ROTATE_270)

    ramka = Image.open("/home/pi/Desktop/ramka1.jpg")
    text_img = Image.new('RGBA', (1200,2000), (0, 0, 0, 0))

    text_img.paste(ramka, (0,0))
    text_img.paste(im, (50,30))
    text_img.save("ramka.png", format="png")
    path = save_location + "/ramka.png"
    top2 = Toplevel(window)
    top2.geometry("1600x720")
    top2.overrideredirect(1)
    top2.title("fotobudka")
    top2.configure(background='black')

    img = ImageTk.PhotoImage(Image.open(path))
    panel = Label(top2, image = img)
    panel.pack(side = "bottom", fill = "both", expand = "yes")
    playerOff()

        #close gui
    window.after(5000, lambda:  top2.destroy())    

#************MAIN

if __name__ == "__main__":          
try:

            #Gui main*********************************

            window = Tk()
            top1 = Toplevel(window)
            top1.geometry("1600x720")
         #  top1.wm_attributes('-topmost',1) #zawsze na wierzchu
            top1.configure(background='black')
            button_start = Button(window, text='start',command=Start)
            button_start.pack()
            window.mainloop()    


        #Gui main end******************************

except KeyboardInterrupt:
        print "koniec"

0 个答案:

没有答案