【问题标题】:saving an image with a transparent background [duplicate]保存具有透明背景的图像[重复]
【发布时间】:2021-08-24 18:41:03
【问题描述】:

我的程序有一些问题:

minimap = pygame.Surface((100, 100))
background = pygame.Surface((minimap.get_width(), minimap.get_height()))
background.fill((0, 0, 0))
background.set_alpha(0)
minimap.blit(background, (0, 0, minimap.get_width(), minimap.get_height()))
pygame.image.save(minimap, "minimap.png")

问题是,我希望背景是透明的,因为我稍后会在其他东西上显示它并且我想看到它的背后,所以我使用了另一个表面,我已经设置了颜色并将 alpha 设置为0,之后背景设置为小地图。

如果你有任何线索,请告诉我

【问题讨论】:

    标签: python image pygame png alpha


    【解决方案1】:

    background 没有每像素 alpha 格式。设置 SRCALPHA 标志以创建具有包含每像素 alpha 的图像格式的表面。不要用不透明的黑色填充背景:

    background = pygame.Surface((minimap.get_width(), minimap.get_height()))
    background.fill((0, 0, 0))
    background.set_alpha(0)

    background = pygame.Surface(minimap.get_size(), pygame.SRCALPHA)
    

    您也可以使用set_colorkey() 设置透明色键

    background = pygame.Surface((minimap.get_width(), minimap.get_height()))
    background.set_colorkey((0, 0, 0)) 
    

    【讨论】:

    • 谢谢你,这个回答了我所有的问题^^
    • @thautot 谢谢。不客气。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-12-05
    • 2012-05-26
    • 2019-10-17
    • 2012-08-21
    • 2014-10-05
    • 1970-01-01
    • 2015-05-15
    相关资源
    最近更新 更多