[Python] How to decrease the brightness of an image in PyGame?

Discussão em 'Python' iniciado por Stack, Abril 19, 2025.

  1. Stack

    Stack Membro Participativo

    I know there's a similar question here - How can I change the brightness of an image in pygame? - but the answer only explains how to increase the brightness, without referring to how to decrease the brightness. I want to create an animation where an image increases in brightness, but then decreases in the same manner. I've tried using this code:

    running = True
    brightness = 1
    brightstep = 1
    while running:
    colour = (brightness, brightness, brightness)
    canvas.fill((255, 255, 255))
    canvas.blit(bg, (0, 0))
    canvas.blit(img, (0, 0))
    if brightness == 51:
    brightstep = -1
    brightness = 50
    elif brightstep == 1:
    brightness += brightstep
    img.fill(colour, special_flags=pygame.BLEND_RGB_ADD)
    elif brightstep == -1:
    brightness += brightstep
    img.fill(colour, special_flags=pygame.BLEND_RGB_SUB)
    for event in pygame.event.get():
    if event.type == pygame.QUIT:
    running = False
    pygame.display.update()


    The images have already been loaded so that's not causing the error. I know it's something to do with the special_flags when I'm decreasing the brightness.

    Continue reading...

Compartilhe esta Página