Python Forum
Thread Rating:
  • 1 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyGame] image color
#7
I made a script to show this example. Without the image you are using i can only use an example image
   

You can change which color is the transparent by change the from_ color. And it shows as white as the background is white before the image is drawn. 
import pygame as pg
   
pg.init()
  
COLORKEY = (0,0,0) #black

RED =    (221,0,0)
ORANGE = (254,98,98)
YELLOW = (254,246,0)
GREEN =  (0,187,0)
BLUE =   (0,155,254)
INDIGO = (0,0,131)
VIOLET = (48,0,155)
    
def swap_color(surf, from_, to_):
    arr = pg.PixelArray(surf)
    arr.replace(from_,to_)
    del arr
   
class Rainbow:
    def __init__(self, screen_rect):
        self.screen_rect = screen_rect
        self.image = pg.image.load('rainbow.png').convert()
        self.rect = self.image.get_rect(center=self.screen_rect.center)
        swap_color(self.image, (0,187,0), COLORKEY)
        self.image.set_colorkey(COLORKEY)
           
    def draw(self, surf):
        surf.blit(self.image, self.rect)
   
screen = pg.display.set_mode((1920,1080))
screen_rect = screen.get_rect()
player = Rainbow(screen_rect)
done = False
while not done:
    screen.fill((255,255,255))
    for event in pg.event.get(): 
        if event.type == pg.QUIT:
            done = True
    player.draw(screen)
    pg.display.update()
    
Recommended Tutorials:
Reply


Messages In This Thread
image color - by Zman350x - Feb-14-2017, 04:36 PM
RE: image color - by nilamo - Feb-14-2017, 08:22 PM
RE: image color - by nilamo - Feb-27-2017, 12:01 AM
RE: image color - by Zman350x - Feb-26-2017, 11:50 PM
RE: image color - by Zman350x - Feb-27-2017, 12:09 AM
RE: image color - by nilamo - Feb-28-2017, 06:30 PM
RE: image color - by metulburr - Mar-03-2017, 01:34 AM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020