如何在Pygame中无头保存透明图像到文件?

2 投票
1 回答
1475 浏览
提问于 2025-04-17 16:19

这是我最近问的一个问题的后续:

在Pygame中,如何在无头模式下保存屏幕图像?

我可以保存包含不透明表面对象的屏幕图像。但是,这种方法对透明表面对象不起作用。以下代码展示了这个问题:

import sys
import os
import pygame
from pygame.color import THECOLORS as RGB


class PygameProblemDemo(object):
    
    def __init__(self):
        screen_width, screen_height = 200, 200
        os.environ['SDL_VIDEODRIVER'] = 'dummy'
        
        pygame.init()
        pygame.display.init()
        
        self.screen = pygame.display.set_mode((screen_width, screen_height))
    
    def save_non_alpha_image(self):
        r = 50
        color = RGB['blue']
        img_path = '/tmp/non_alpha_image.png'
        
        background = pygame.Surface((200, 200), pygame.SRCALPHA, 32)
        pygame.draw.rect(background, RGB['lightgray'], (0, 0, 200, 200), 0)
        
        self.screen.fill(RGB['black'])
        self.screen.blit(background, (0,0))
        pygame.draw.circle(self.screen, color, (100,100), r, 0)
        
        pygame.image.save(self.screen, img_path)
        print "image saved to %s" % (img_path)
    
    def save_alpha_image(self):
        r = 50
        color = RGB['blue']
        img_path = '/tmp/alpha_image.png'
        
        background = pygame.Surface((200, 200), pygame.SRCALPHA, 32)
        pygame.draw.rect(background, RGB['lightgray'], (0, 0, 200, 200), 0)
        
        transparent_circle = self.draw_transparent_circle(r, color, 50)
        
        self.screen.fill(RGB['black'])
        self.screen.blit(background, (0,0))
        self.screen.blit(transparent_circle, (50,50))
        
        pygame.image.save(self.screen, img_path)
        print "image saved to %s" % (img_path)
    
    def draw_transparent_circle(self, radius, color, transparency=0):
        """transparency is value between 0 and 100, 0 is opaque,
        100 invisible"""
        width, height = radius*2, radius*2
        
        # transparent base surface
        flags = pygame.SRCALPHA
        depth = 32
        base = pygame.Surface((width, height), flags, depth)
        
        # alpha surface
        alpha = int(round(255 * (100-transparency) / 100.0))
        alpha_surface = pygame.Surface((width, height))
        alpha_surface.set_colorkey(RGB['black'])
        alpha_surface.set_alpha(alpha)
        
        # draw circle (to alpha surface)
        pygame.draw.circle(alpha_surface, color, (radius,radius), radius)
        
        # draw alpha surface to base surface
        base.blit(alpha_surface, (0,0))
        
        return base
    
    
demo = PygameProblemDemo()
demo.save_non_alpha_image()
demo.save_alpha_image()

这是结果:

non_alpha_image.png

non_alpha_image.png

alpha_image.png

alpha_image.png


更新

如果我在构造函数中注释掉这一行 os.environ['SDL_VIDEODRIVER'] = 'dummy',透明图像就能成功保存:

alpha_image.png

alpha_image.png

不过,这一行是用来防止窗口打开的,所以脚本就不能再无头运行了。 :\

1 个回答

1

我搞明白了。Pygame的显示需要开启透明通道。要修复我上面的例子,只需在构造函数中把这一行替换成:

self.screen = pygame.display.set_mode((screen_width, screen_height))

换成这个:

self.screen = pygame.display.set_mode((screen_width, screen_height), 0, 32)

撰写回答