如何捕获pygame屏幕?
如何捕获并保存 pygame 屏幕的一系列图像或视频?
基本上我想在 youtube 上分享我的游戏视频。另外,想做个教程。
游戏主要以循环方式渲染:
def main():
while True:
GetInput()
Move()
Shift()
Draw()
使用 Draw()
函数在执行 pygame.display.flip() 之前执行所有
blit()
等操作
How can I capture and save a sequence of images or a video of a pygame screen?
Basically I want to share my game video on youtube. Also, want to make a tutorial.
The game is rendered mainly in a loop:
def main():
while True:
GetInput()
Move()
Shift()
Draw()
With the Draw()
function doing all the blit()
and stuff before doing the pygame.display.flip()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在屏幕表面使用 pygame.image.save :
请注意,这会极大地减慢您的程序速度。如果它是基于时间的,您可能希望在捕获时伪造帧速率。
Use
pygame.image.save
on your screen surface:Note that this will slow down your program tremendously. If it is time-based, you may wish to fake the framerate when doing a capture.
接受的答案说您可以使用 pygame.image.save 方法保存当前屏幕
这是一个好主意,但是您可能不希望在游戏运行时将图像保存在硬盘上,问题中也指出。相反,您应该将屏幕保存在程序中,然后在游戏停止运行后处理它们。
这是我的带有注释的代码,仅显示了屏幕录制如何工作的总体思路。它使用 opencv(带有 numpy)和 pygame,但您必须安装 ffmpeg 将图像转换为视频(尝试在终端中使用 ffmpg 进行测试)。不要让程序运行太长时间,因为保存仍然需要相当长的时间,并且大约与记录的帧成正比。为了提高效率,您只能每隔一帧左右录制一次。
或者您只需使用 Pygame Recorder 之类的库。
The accepted answer said you could save the current screen with the
pygame.image.save
methodThis is a good idea, however you might not want to save the images on the hard drive while the game is running, also pointed out by the question. Instead, you should save the screens in the program and then process them after the game has stopped running.
Here is my code with comments, that only shows the general idea of how a screen recording might work. It uses opencv (with numpy) and pygame but you have to install ffmpeg to convert the images to a video (try
ffmpg
in the terminal to test). Don't let the program run too long, because the saving still takes quite a while and is about proportional to the recorded frames. For more efficiency, you could only record every second frame or so.Or you just use a lib like Pygame Recorder.
我找到了一个很酷的方法;
您可以使用它
来获取任何表面或屏幕上的所有像素(窗口变量)!
您可以在 NumPy 之类的东西中使用它,您可以使用代码
将表面图像作为 NumPy 数组获取,然后
根据需要将其设为 Pillow 图像。然后您可以使用简单的
im.show()
来显示它,或者只是用它做任何事情。I found a cool way;
you can use
to get all the pixels on any surface or the screen (the window variable)!
You can use this in things like NumPy where you can use the code
to get the image of the surface as a NumPy array and then
to make it a Pillow image, if you want. Then you can show it with a simple
im.show()
or just do whatever with it.