From 613b868121d53c986add525bde165e8bb87d7e14 Mon Sep 17 00:00:00 2001 From: littlefean <2028140990@qq.com> Date: Wed, 18 Sep 2024 21:54:32 +0800 Subject: [PATCH] init the project --- .gitignore | 2 ++ README.md | 3 +++ main.py | 64 ++++++++++++++++++++++++++++++++++++++++++++++++ requirements.txt | 1 + 4 files changed, 70 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 main.py create mode 100644 requirements.txt diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..47afe17 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.venv/ +output/ \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..ab07383 --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ + + +pillow 10.4.0 \ No newline at end of file diff --git a/main.py b/main.py new file mode 100644 index 0000000..20d319f --- /dev/null +++ b/main.py @@ -0,0 +1,64 @@ +import os +import asyncio +from time import perf_counter +import subprocess + +from PIL import ImageGrab + + +async def grab_screenshot(file_name="screenshot.png"): + # 截图并保存 + t1 = perf_counter() + im = ImageGrab.grab() + im.save(file_name) + t2 = perf_counter() + print(f"截图耗时:{t2 - t1:.2f}秒") + # 经测试大约耗时0.3秒 + + +def images_to_video(dir_name, video_name="timelapse.mp4"): + # 将图片合成视频 + # 使用ffmpeg将图片合成视频 + command = [ + "ffmpeg", + "-framerate", + "30", # 设置帧率 + "-i", + f"{dir_name}/%d.png", # 输入文件的路径 + "-c:v", + "libx264", # 设置视频编码格式 + "-pix_fmt", + "yuv420p", # 设置像素格式 + video_name, # 输出视频文件名 + ] + + # 调用ffmpeg命令 + subprocess.run(command) + print(f"视频已保存为 {video_name}") + pass + + +async def main(): + input("按任意键开始截图...") + + # 先在当前文件夹下创建一个文件夹 + output_dir = "output" + if not os.path.exists(output_dir): + os.mkdir(output_dir) + + # 文件夹名字以时间格式 + dir_name = f"screenshots-{int(perf_counter())}" + + if not os.path.exists(f"{output_dir}/{dir_name}"): + os.mkdir(dir_name) + + for i in range(10): + print(f"第{i+1}次截图") + asyncio.create_task(grab_screenshot(f"{dir_name}/{i+1}.png")) + await asyncio.sleep(1) + + pass + + +if __name__ == "__main__": + asyncio.run(main()) diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..4efc8c7 --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +pillow==10.4.0