![](/img/trans.png)
[英]How to make a cut-out/make the background of an image transparent using opencv?
[英]How to blit a tranparent image in pygame to make an alpha “cut-out”
给定一个充满一些像素的pygame曲面,我想以编程方式在该图像中创建一个Alpha通道“窗口”(这样背景就会显示出来)。
显然,我意识到可以使用位图编辑器轻松完成此操作,问题的关键是作为不同,更复杂的操作的一部分即时进行此操作。
因此,我制作了一个在“全红”和“带孔的红色”图像之间切换的精灵,并尝试通过将完全透明的图像涂抹到基本位图上来动态创建位图窗口。
我认为正在发生的是,PyGame没有绘制完全透明的像素,因为它们是透明的! 有什么方法可以使它们变薄,从而制成窗户吗?
我尝试添加pygame.BLEND_RGBA_MIN
和其他 (根据“ 需要在Pygame中 pygame.BLEND_RGBA_MIN
表面透明性的建议”),但未能获得所需的结果。
我得到的只是一个以红色为中心的黑色正方形,而不是背景的窗口。
import pygame
# Window size
WINDOW_WIDTH = 400
WINDOW_HEIGHT = 400
FPS = 60
# background colours
INKY_BLACK = (128, 128, 128)
class HoleSprite( pygame.sprite.Sprite ):
def __init__( self ):
pygame.sprite.Sprite.__init__( self )
# Make a full-image (no hole)
self.base_image = pygame.Surface( ( 32, 32 ), pygame.SRCALPHA )
self.base_image.fill( ( 255,0,0 ) )
# Make an image with a see-through window
self.hole_image = pygame.Surface( ( 32, 32 ), pygame.SRCALPHA )
self.hole_image.fill( ( 255,0,0 ) )
self.hole = pygame.Surface( ( 10, 10 ), pygame.SRCALPHA )
self.hole.fill( ( 0, 0, 0, 255 ) ) # transparent
self.hole_image.blit( self.hole, [ 10,10, 10,10 ] ) # punch the middle out?
# sprite housekeeping
self.image = self.base_image
self.rect = self.image.get_rect()
self.rect.x = WINDOW_WIDTH // 2 # centred
self.rect.y = WINDOW_HEIGHT // 2
self.last = 0
def update( self ):
time_ms = pygame.time.get_ticks()
# FLip the images each second
if ( time_ms - self.last > 1000 ):
if ( self.image == self.hole_image ):
self.image = self.base_image
else:
self.image = self.hole_image
self.last = time_ms
### MAIN
pygame.init()
pygame.font.init()
SURFACE = pygame.HWSURFACE|pygame.DOUBLEBUF|pygame.RESIZABLE
WINDOW = pygame.display.set_mode( ( WINDOW_WIDTH, WINDOW_HEIGHT ), SURFACE )
pygame.display.set_caption("Hole Srite Test")
anims = pygame.sprite.GroupSingle()
holey = HoleSprite( )
anims.add( holey )
clock = pygame.time.Clock()
done = False
while not done:
# Handle user-input
for event in pygame.event.get():
if ( event.type == pygame.QUIT ):
done = True
# Repaint the screen
anims.update()
WINDOW.fill( INKY_BLACK )
anims.draw( WINDOW )
pygame.display.flip()
# Update the window, but not more than 60fps
clock.tick_busy_loop( FPS )
pygame.quit()
首先:您需要(0,0,0,0)
而不是(0,0,0,255)
第二:您必须使用fill(color, rect)
直接在红色矩形中创建透明孔。
self.hole_image.fill( (0, 0, 0, 0), (10, 10, 10, 10) )
或者您可以直接在红色表面上绘制透明矩形
pygame.draw.rect(self.hole_image, (0, 0, 0, 0), (10, 10, 10, 10) )
当您在全彩色图像上对透明图像进行blit时,它不会替换像素,但会同时混合两种颜色(透明和全色),您会获得全彩色。
blit()
具有标志BLEND_ADD
, BLEND_SUB
, BLEND_MULT
等,也许使用此标志之一可以替换像素,但我从未尝试过。
其他信息: pygame透明度
完整代码:
import pygame
# Window size
WINDOW_WIDTH = 400
WINDOW_HEIGHT = 400
FPS = 60
# background colours
INKY_BLACK = (128, 128, 128)
class HoleSprite( pygame.sprite.Sprite ):
def __init__( self ):
pygame.sprite.Sprite.__init__( self )
# Make a full-image (no hole)
self.base_image = pygame.Surface( ( 32, 32 ), pygame.SRCALPHA )
self.base_image.fill( ( 255,0,0 ) )
# Make an image with a see-through window
self.hole_image = pygame.Surface( ( 32, 32 ), pygame.SRCALPHA )
self.hole_image.fill( ( 255,0,0 ) )
self.hole_image.fill( (0, 0, 0, 0), (10, 10, 10, 10) )
# OR
#pygame.draw.rect(self.hole_image, (0, 0, 0, 0), (10, 10, 10, 10) )
# sprite housekeeping
self.image = self.base_image
self.rect = self.image.get_rect()
self.rect.x = WINDOW_WIDTH // 2 # centred
self.rect.y = WINDOW_HEIGHT // 2
self.last = 0
def update( self ):
time_ms = pygame.time.get_ticks()
# FLip the images each second
if ( time_ms - self.last > 1000 ):
if ( self.image == self.hole_image ):
self.image = self.base_image
else:
self.image = self.hole_image
self.last = time_ms
### MAIN
pygame.init()
pygame.font.init()
SURFACE = pygame.HWSURFACE|pygame.DOUBLEBUF|pygame.RESIZABLE
WINDOW = pygame.display.set_mode( ( WINDOW_WIDTH, WINDOW_HEIGHT ), SURFACE )
pygame.display.set_caption("Hole Srite Test")
anims = pygame.sprite.GroupSingle()
holey = HoleSprite( )
anims.add( holey )
clock = pygame.time.Clock()
done = False
while not done:
# Handle user-input
for event in pygame.event.get():
if ( event.type == pygame.QUIT ):
done = True
# Repaint the screen
anims.update()
WINDOW.fill( INKY_BLACK )
anims.draw( WINDOW )
pygame.display.flip()
# Update the window, but not more than 60fps
clock.tick_busy_loop( FPS )
pygame.quit()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.