Python Pygame中的透明图像
我在Pygame中有一个精灵,它是一个蓝色的圆圈。我希望将此图像绘制到“褪色”屏幕上,例如半透明。然而,我不想在它上面画一个半透明的矩形;相反,我希望修改实际图像并使其半透明。非常感谢您的帮助 现在我有:Python Pygame中的透明图像,python,image,pygame,Python,Image,Pygame,我在Pygame中有一个精灵,它是一个蓝色的圆圈。我希望将此图像绘制到“褪色”屏幕上,例如半透明。然而,我不想在它上面画一个半透明的矩形;相反,我希望修改实际图像并使其半透明。非常感谢您的帮助 现在我有: Class Circle(pygame.sprite.Sprite): self.image = self.image = pygame.image.load("circle.png") circle = Circle() 最终 window.blit(pyg
Class Circle(pygame.sprite.Sprite):
self.image = self.image = pygame.image.load("circle.png")
circle = Circle()
最终
window.blit(pygame.transform.scale(circle.image, (zoom, zoom)), (100, 100))
circle.png的外观:
使图像透明后我希望图像的外观:
我正在将图像快速切换到白色背景的窗口上。使用每像素alpha创建一个新曲面
surf = pygame.Surface((circle_width, circle_height), pygame.SRCALPHA)
使表面透明
surf.set_alpha(128) # alpha value
在(x=0,y=0)
将曲面绘制到窗口
window.blit(surf, (circle_x, circle_y))
正如文档所说,可以使用“齐次alpha”或每像素alpha,但不能同时使用这两种方法,我相信这正是您想要的。它可能适用于颜色键透明度,但我不确定(我还没有测试过)。如果在闪烁之前使用set_colorkey()
和set_alpha()
,则任何非RGBA(无活动alpha通道)像素格式都可能工作
因此,代码可能如下所示:
class Circle(pygame.sprite.Sprite):
def __init__(self)
self.image = pygame.image.load("circle.png")
# get the surface's top-left pixel color and use it as colorkey
colorkey = self.image.get_at((0, 0))
self.image.set_colorkey(colorkey)
在代码中的某个时刻(渲染之前),您可能希望通过调用以下命令来设置透明度:
circle.image.set_alpha(some_int_val)
然后,您可以根据需要缩放和blit它。首先,您的图像/曲面需要使用每像素alpha,因此在加载时调用该方法。如果要创建新曲面(如示例中所示),还可以将
pygame.SRCALPHA
传递到pygame.surface
第二步是创建另一个曲面(此处称为alpha_surface
),用白色和所需的alpha值(颜色元组的第四个元素)填充该曲面
最后,您必须将alpha_曲面
blit到图像上,并将pygame.BLEND_RGBA_MULT
作为特殊标志
参数传递。那会让你
图像的不透明部分是半透明的
import pygame as pg
pg.init()
screen = pg.display.set_mode((800, 600))
clock = pg.time.Clock()
BLUE = pg.Color('dodgerblue2')
BLACK = pg.Color('black')
# Load your image and use the convert_alpha method to use
# per-pixel alpha.
# IMAGE = pygame.image.load('circle.png').convert_alpha()
# A surface with per-pixel alpha for demonstration purposes.
IMAGE = pg.Surface((300, 300), pg.SRCALPHA)
pg.draw.circle(IMAGE, BLACK, (150, 150), 150)
pg.draw.circle(IMAGE, BLUE, (150, 150), 130)
alpha_surface = pg.Surface(IMAGE.get_size(), pg.SRCALPHA)
# Fill the surface with white and use the desired alpha value
# here (the fourth element).
alpha_surface.fill((255, 255, 255, 90))
# Now blit the transparent surface onto your image and pass
# BLEND_RGBA_MULT as the special_flags argument.
IMAGE.blit(alpha_surface, (0, 0), special_flags=pg.BLEND_RGBA_MULT)
done = False
while not done:
for event in pg.event.get():
if event.type == pg.QUIT:
done = True
screen.fill((50, 50, 50))
pg.draw.rect(screen, (250, 120, 0), (100, 300, 200, 100))
screen.blit(IMAGE, (150, 150))
pg.display.flip()
clock.tick(60)
pg.quit()
可以提供
circle.png
,您正在绘制它的背景,以及显示您希望它看起来如何的第三个图像。一张图片抵得上千言万语,因此3张图片将为您节省大量文字。我添加了图片以向您展示我需要的内容。尽管我决定在我的程序中不使用透明度,但这正是我想要做的。非常感谢。
import pygame as pg
pg.init()
screen = pg.display.set_mode((800, 600))
clock = pg.time.Clock()
BLUE = pg.Color('dodgerblue2')
BLACK = pg.Color('black')
# Load your image and use the convert_alpha method to use
# per-pixel alpha.
# IMAGE = pygame.image.load('circle.png').convert_alpha()
# A surface with per-pixel alpha for demonstration purposes.
IMAGE = pg.Surface((300, 300), pg.SRCALPHA)
pg.draw.circle(IMAGE, BLACK, (150, 150), 150)
pg.draw.circle(IMAGE, BLUE, (150, 150), 130)
alpha_surface = pg.Surface(IMAGE.get_size(), pg.SRCALPHA)
# Fill the surface with white and use the desired alpha value
# here (the fourth element).
alpha_surface.fill((255, 255, 255, 90))
# Now blit the transparent surface onto your image and pass
# BLEND_RGBA_MULT as the special_flags argument.
IMAGE.blit(alpha_surface, (0, 0), special_flags=pg.BLEND_RGBA_MULT)
done = False
while not done:
for event in pg.event.get():
if event.type == pg.QUIT:
done = True
screen.fill((50, 50, 50))
pg.draw.rect(screen, (250, 120, 0), (100, 300, 200, 100))
screen.blit(IMAGE, (150, 150))
pg.display.flip()
clock.tick(60)
pg.quit()