gpt4 book ai didi

python - 为什么我的python脚本输出文件为空

转载 作者:行者123 更新时间:2023-12-02 16:09:25 25 4
gpt4 key购买 nike

我正在创建一个 python 脚本来记录桌面屏幕。

在这个我只取用户选择的区域,只记录选择的区域并将其转换为视频文件。

但是在执行此操作时,脚本的输出是一个空的视频文件(0 字节)

谁能告诉我我在哪里做错了。因为它在没有 bbox(x1,x2,y1,y2) 的情况下工作正常.

这是代码:
编辑:

import tkinter as tk
from tkinter import *
from tkinter import ttk ,FLAT
from PIL import Image, ImageTk, ImageGrab, ImageEnhance
import cv2
import numpy as np
import threading
filename="test.avi"
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
frame_rate = 10
root = tk.Tk()


def show_image(image):
win = tk.Toplevel()
win.image = ImageTk.PhotoImage(image)
tk.Label(win, image=win.image).pack()
win.grab_set()
win.wait_window(win)

def area_sel():
x1 = y1 = x2 = y2 = 0
roi_image = None

def on_mouse_down(event):
nonlocal x1, y1
x1, y1 = event.x, event.y
canvas.create_rectangle(x1, y1, x1, y1, outline='red', tag='roi')

def on_mouse_move(event):
nonlocal roi_image, x2, y2
x2, y2 = event.x, event.y
canvas.delete('roi-image')
roi_image = image.crop((x1, y1, x2, y2))
canvas.image = ImageTk.PhotoImage(roi_image)
canvas.create_image(x1, y1, image=canvas.image, tag=('roi-image'), anchor='nw')
canvas.coords('roi', x1, y1, x2, y2)
canvas.lift('roi')

root.withdraw()
image = ImageGrab.grab()
bgimage = ImageEnhance.Brightness(image).enhance(0.3)
win = tk.Toplevel()
win.attributes('-fullscreen', 1)
win.attributes('-topmost', 1)
canvas = tk.Canvas(win, highlightthickness=0)
canvas.pack(fill='both', expand=1)
tkimage = ImageTk.PhotoImage(bgimage)
canvas.create_image(0, 0, image=tkimage, anchor='nw', tag='images')
win.bind('<ButtonPress-1>', on_mouse_down)
win.bind('<B1-Motion>', on_mouse_move)
win.bind('<ButtonRelease-1>', lambda e: win.destroy())
win.focus_force()
win.grab_set()
win.wait_window(win)
root.deiconify()

if roi_image:
region = x1, y1, x2, y2

start_recording(region) #calling main function to record screen
return region


def recording_screen(x1, y1, x2, y2):
global recording
recording = True

while recording:

img = ImageGrab.grab(bbox=(x1, y1, x2, y2))
frame=np.array(img) # for recording
out.write(cv2.cvtColor(frame,cv2.COLOR_RGB2BGR))

out = cv2.VideoWriter()
def start_recording(region):
x1,y1,x2,y2 = region
if not out.isOpened():

out.open(filename,fourcc, frame_rate,(x2-x1,y2-y1))
threading.Thread(target=recording_screen, args=region, daemon=True).start()

def stop_recording():
global recording
recording = False
out.release()

sel_area = ttk.Button(root, text='select area to Record', width=30, command=area_sel)
sel_area.grid(row=0, column=0)

stp_rec = ttk.Button(root, text='Stop Recording', width=30, command=stop_recording)
stp_rec.grid(row=0, column=1)

root.mainloop()

最佳答案

您正在使用 ImageGrab仅适用于 macOS 和 Windows,因此我无法在我的设置中测试您的完整代码。

您的代码中的问题是您已将区域选择和视频录制结合起来。选择区域后,您将拥有 x1 , y1 , x2 , 和 y2 .返回这些值。初始化cv2.VideoWriter第一次。

您现在可以尝试的是使用传递给 start_recording((x1, y1, x2, y2)) 的参数。 .

def start_recording(region):
x1,y1,x2,y2 = region
if not out.isOpened():

out.open(filename,fourcc, frame_rate,(y2-y1,x2-x1)) #given x2 > x1, y2 > y1
threading.Thread(target=recording_screen, args=region, daemon=True).start()

编辑:

问题是在以下调用中传递了错误的形状。
out.open(filename,fourcc, frame_rate,(y2-y1,x2-x1))
Size传递的值为 (y2-y1,x2-x1)(height, width) .但是,构造函数需要 (width, height) .见文档 here .

此外, img = ImageGrab.grab(bbox=(x1, y1, x2, y2))返回 RGB image .但是 out.write(frame)预计 BGR image .它可以固定为:
img = ImageGrab.grab(bbox=(x1, y1, x2, y2))
frame = np.array(img)
out.write(cv2.cvtColor(frame, cv2.COLOR_RGB2BGR))

关于python - 为什么我的python脚本输出文件为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61009265/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com