【问题标题】:How to take screenshot of certain part of screen in Pygame如何在 Pygame 中截取屏幕的某些部分
【发布时间】:2013-06-20 11:35:22
【问题描述】:

有没有办法截取我的 pygame 窗口的右半部分?

我正在使用 pygame 制作游戏,我需要拍摄屏幕快照,而不是整个屏幕,只是右半部分。

我知道:

pygame.image.save(screen,"screenshot.jpg")

但这将包括图像中的整个屏幕。

有没有办法截取我的 pygame 窗口的右半部分?

也许通过以某种方式更改它包含的区域?我用谷歌搜索了它,但找不到任何我想的东西,也许我可以使用 PIL 来裁剪它,但这似乎需要做很多额外的工作。

如果不可能,谁能告诉我裁剪整个屏幕图片的最简单方法?

【问题讨论】:

    标签: python pygame screenshot crop


    【解决方案1】:
    import pygame
    import sys
    
    
    screen = pygame.display.set_mode((400, 500))
    clock = pygame.time.Clock()
    
    
    def grab(x, y, w, h):
        "Grab a part of the screen"
        # get the dimension of the surface
        rect = pygame.Rect(x, y, w, h)
        # copy the part of the screen
        sub = screen.subsurface(rect)
        # create another surface with dimensions
        # This is done to unlock the screen surface
        screenshot = pygame.Surface((w, h))
        screenshot.blit(sub, (0, 0))
        return screenshot
    
    
    def blit(part, x, y):
        screen.blit(part, (x, y))
    
    
    def quit():
        pygame.quit()
        sys.exit()
    
    
    def start():
        # shows half the screen
        blit(back, 0, 0)
        # and the other half copied
        sub = grab(50, 0, 75, 250)
        blit(sub, 200, 0)
        while True:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    quit()
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_ESCAPE:
                        quit()
            pygame.display.update()
            clock.tick(60)
    
    
    back = pygame.image.load("img\\back.png")
    
    start()
    

    【讨论】:

      【解决方案2】:

      这并不完全适用于我的 Python 3.7.4 系统。这是一个有效的版本:

      rect = pygame.Rect(25, 25, 100, 50)
      sub = screen.subsurface(rect)
      screenshot = pygame.Surface((100, 50))
      screenshot.blit(sub, (0,0))
      pygame.image.save(screenshot, "screenshot.jpg")
      

      【讨论】:

        【解决方案3】:

        我会这样做:

        example = pygame.Surface(screen.get_width()/2, 0)

        然后当你想截图的时候做:

        pygame.image.save(example, "example.jpg")

        【讨论】:

          【解决方案4】:

          如果您总是希望屏幕截图位于屏幕的同一部分,您可以使用subsurfacehttp://www.pygame.org/docs/ref/surface.html#pygame.Surface.subsurface

          rect = pygame.Rect(25, 25, 100, 50)
          sub = screen.subsurface(rect)
          pygame.image.save(sub, "screenshot.jpg")
          

          subsurface 在这种情况下会很好地工作,因为对父表面(在这种情况下为screen)的任何更改也将应用于次表面。

          如果您希望能够指定屏幕的任意部分来截取屏幕截图(因此,每次都不是同一个矩形),那么最好创建一个新表面,对所需部分进行 blit屏幕到该表面,然后保存。

          rect = pygame.Rect(25, 25, 100, 50)
          screenshot = pygame.Surface(100, 50)
          screenshot.blit(screen, area=rect)
          pygame.image.save(screenshot, "screenshot.jpg")
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2023-03-31
            • 1970-01-01
            • 1970-01-01
            • 2011-03-15
            • 1970-01-01
            • 2021-08-24
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多