【问题标题】:How to take screenshot of entire display pygame如何截取整个显示pygame的屏幕截图
【发布时间】:2020-08-10 14:43:36
【问题描述】:

我正在创建一个画家软件,并希望用户在完成创建后能够保存他们的图像,所以我尝试了pygame.image.save(pygame.display.get_surface(), "/home/user/screenshot.png")。我快速绘图并按下我设置的保存图像的键。我看了一下图像,它只保存了空白显示表面,而不是实际绘图的pygame.draw.rect()s。我查看了以下链接:How to capture pygame screen?https://gamedev.stackexchange.com/questions/118372/how-can-i-take-a-screenshot-of-a-certain-part-of-the-screen-in-pygameCan python get the screen shot of a specific window? 等等。我将如何截取整个显示屏的屏幕截图以及绘图?这是我的主循环:

running = True
while running:
    updateWindow() # Updates window
    clearWindow() # Clears window
    checkEvents() # Checks events
    redrawItems() # Redraws your drawing
    pygame.event.pump()
pygame.display.quit()
pygame.quit()

【问题讨论】:

标签: python python-3.x pygame screenshot screen-capture


【解决方案1】:

在显示表面上尝试pygame.Surface.copy。请参阅文档here

所以如果display 是你的屏幕,那么:

screencopy = display.copy()

应该在screencopy 中为您提供显示图像的副本。请记住,由于双缓冲,如果您当时执行display.update(),它将为您提供屏幕上看到的内容的副本,如果您做了尚未完成的事情,这可能与屏幕上显示的内容不同由update() 推送到屏幕上。

【讨论】:

    【解决方案2】:

    您可以使用 pygame.image.save(Surface, filename) 来执行此操作,您可以阅读更多关于 here

    下面是一个简单的函数,它将显示的一部分保存为图像。

    def Capture(display,name,pos,size): # (pygame Surface, String, tuple, tuple)
        image = pygame.Surface(size)  # Create image surface
        image.blit(display,(0,0),(pos,size))  # Blit portion of the display to the image
        pygame.image.save(image,name)  # Save the image to the disk**
    

    这个函数的作用是创建一个名为 image 的 pygame 表面。然后区域 (pos,size) 在其原点被 blitted 到图像。最后调用pygame.image.save(Surface, filename),将图片保存到磁盘。

    例如,如果我们想在显示器上的 50x50 位置保存一个名为“Capture.png”的 100x100 图像,名称将等于“Capture.png”,位置等于 (50,50),大小等于(100,100),函数调用如下所示:

    Capture(display,"Capture.png",(50,50),(100,100))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-05-10
      • 2021-04-16
      • 2010-11-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多