Hi, I wrote some code using pygame that has two images. I am trying to figure out how transparency works, so I have tried to display a circle png on a square png. My code looks like this.

import pygame
from pygame.locals import *



    #make window
    pygame.init()
    screen = pygame.display.set_mode((1270, 790), pygame.FULLSCREEN)
    pygame.display.set_caption('Pygame Caption')

    #load images
    backg = pygame.image.load('backg.png')

    circle = pygame.image.load('circle.png')
    #attempt to add transparency
    circle = circle.convert()
    circle = circle.convert_alpha()

    while True:
    #close program withescape key
        for event in pygame.event.get():
            if (event.type == KEYDOWN):
                print event
                if (event.key == K_ESCAPE):
                    done = True
    #blit image to screen
        screen.blit(backg, (0, 0))
        screen.blit(cirlce, (100, 0))
    #update screen
        pygame.display.update()

The color around the circle I am trying to display has a white background. I thought that
circle = cirlce.convert_alpha() was supposed to make all white backgrounds transparent, but i guess not or it's just an error in my code. Thanks in advance.

Use this ...

# set alpha to 0 (fully transparent) to 255 (not transparent)
circle.set_alpha(150)

How do you make a certain color in an image transparent (ex. An image has a black stick figure (0,0,0) and the background is white (255,255,255) how do you make the white part transparent obis there not a way? Thanks btw for the answer!

To set a particular color transparent use ...

# set a particular color fully transparent
white = (255, 255, 255)
circle.set_colorkey(white)

Thanks sooooo much!!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.