[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

RE: [pygame] loss of transparency in scaling surfaces




Thanks Lenard, I used colorkey with per-pixel alpha that is working well. 


Thanks
Sibtey






> Hi
>
>  I am drawing a line on pygame screen and created one transparent 
> surface of size (800,700) and scaling down using smoothscale to the 
> pygame screen size .In this condition everything is working fine.
>
>  
>
> If have created the transparent surface of size like 4892X3614 and 
> then scaling down to the pygame screen size then transparency is lost. 
> Can any one help me understanding this? The script is shown below
>
>  
>
>  
>
>  
>
> import pygame
>
> BACKGROUNDCOLOR = (255,255,255)
>
[snip code]
>
> # for large surface like this transparecy is Lost
>
> surf = pygame.Surface((4892,3614))
>
> surf.fill(BACKGROUNDCOLOR)
>
> # set transparency
>
> surf.set_colorkey(BACKGROUNDCOLOR,pygame.RLEACCEL)
>
> #draw a line on transparent surface
>
> pygame.draw.line(surf,(200,155,155), (550,550),(750,550),2)
>
> #scale down
>
> surf3  = pygame.transform.smoothscale(surf, (800,700))
>
> screen.blit(surf3,(0,0))
>
>  
>
> pygame.display.flip()
>
[snip code]

The problem is that smoothscale doesn't guarantee colors will remain 
unchanged. I found the background color of surf3 was (253, 253, 253, 
255). The following modified version code does make surf3's background 
transparent.

import pygame
import sys
BACKGROUNDCOLOR = (255,255,255)
pygame.display.init()
screen = pygame.display.set_mode((800,700))
screen.fill(BACKGROUNDCOLOR)
# draw a line on pygame screen
pygame.draw.line(screen,(255,0,0), (50,50),(100,100),1)
# for small surface like this transparecy is fine
## *************************************************
##surf = pygame.Surface((800,700))
## **************************************************************
# for large surface like this transparecy is Lost
surf = pygame.Surface((4892,3614), 0, 24)
surf.fill(BACKGROUNDCOLOR)
#draw a line on transparent surface
pygame.draw.line(surf,(200,155,155), (550,550),(750,550),2)
#scale down
surf3  = pygame.transform.smoothscale(surf, (800,700))
#set transparency
surf3.set_colorkey(surf3.get_at((0,0)),pygame.RLEACCEL)
screen.blit(surf3,(0,0))
 
pygame.display.flip()
while 1:
    events = pygame.event.get()
    for event in events:
        if event.type==pygame.KEYDOWN and event.key ==pygame.K_ESCAPE:
            pygame.display.quit()
            sys.exit()


But it also shows that transparency and scaling don't go together well. 
Using colorkey with per-pixel alpha may be a better solution:

....
surf = pygame.Surface((4892,3614), pygame.SRCALPHA, 32)
bg = BACKGROUNDCOLOR
surf.fill((bg[0],bg[1],bg[2],0)) # Transparent background color.
....
surf3  = pygame.transform.smoothscale(surf, (800,700))
screen.blit(surf3,(0,0))
....

-- 
Lenard Lindstrom
<len-l@xxxxxxxxx>