png images with transparent background don't work in pygame 2.0.0

JohnMillimetron

When I load my png image with transparent background as a sprite image there are very weird background on it. I used Photoshop to draw the image.

Also, there are an error: libpng warning: iCCP: known incorrect sRGB profile

It appears only when I close the pygame window.

I attach my code, png image and screenshot of how does it works below.

import os
import pygame

FPS = 50
WIDTH, HEIGHT = 500, 500

pygame.init()

size = width, height = WIDTH, HEIGHT
screen = pygame.display.set_mode(size)

all_sprites = pygame.sprite.Group()
player_group = pygame.sprite.Group()


def load_image(name, color_key=None):
    fullname = os.path.join('textures', name)
    try:
        image = pygame.image.load(fullname).convert()
    except pygame.error as message:
        print('Cannot load image:', name)
        raise SystemExit(message)

    if color_key is not None:
        if color_key == -1:
            color_key = image.get_at((0, 0))
        image.set_colorkey(color_key)
    else:
        image = image.convert_alpha()
    return image


class Player(pygame.sprite.Sprite):
    def __init__(self, pos_x, pos_y):
        super().__init__(player_group, all_sprites)
        self.image = pygame.transform.scale(load_image('baloon.png', 1), (42, 94))

        self.rect = self.image.get_rect().move(pos_x, pos_y)

    def update(self, *args):
        pass


clock = pygame.time.Clock()
player = Player(200, 200)
running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
    screen.fill(pygame.Color("#AAAAAA"))
    all_sprites.draw(screen)

    all_sprites.update()
    pygame.display.flip()
    clock.tick(FPS)
pygame.quit()

baloon.png

this is how does it seen

Rabbid76

PNGs have per pixel alpha. It is not necessary to set a color key. Simply blit the protector on the tank. convert() changes the pixel format to a format without alpha per pixel and removes the transparency information. Use convert_alpha() instead:

class Player(pygame.sprite.Sprite):
    def __init__(self, pos_x, pos_y):
        super().__init__(player_group, all_sprites)
        slef.image = pygame.image.load('baloon.png').convert_alpha()
        self.image = pygame.transform.scale(slef.image, (42, 94))


I suggest to modify the load_image function:

def load_image(name, per_pixel_alpha = False, color_key = None):
    fullname = os.path.join('textures', name)
    try:
        image = pygame.image.load(fullname)
    except pygame.error as message:
        print('Cannot load image:', name)
        raise SystemExit(message)

    if per_pixel_alpha:
        image = image.convert_alpha()
    else:
        image = image.convert()

    if color_key is not None:
        if color_key == -1:
            color_key = image.get_at((0, 0))
        image.set_colorkey(color_key)
   
    return image  
class Player(pygame.sprite.Sprite):
    def __init__(self, pos_x, pos_y):
        super().__init__(player_group, all_sprites)
        self.image = pygame.transform.scale(load_image('baloon.png', True), (42, 94))

        self.rect = self.image.get_rect().move(pos_x, pos_y)

    def update(self, *args):
        pass 

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Replacing transparent background with white color in PNG images

Issue centering 2 overlaying images (transparent png)

background: transparent; vs background: 0 0; differences and threats

Button hover don't work with transparent

Why transparent background doesn't work with GD?

Python Can't load background images in pygame

Issue with transparent images with a transparent background

Postgres SUM returns 0 rows when columns don't exist. COALESCE and ISNULL don't work

my <p> tags don't work when using *{margin:0; padding:0;}

The background images don't cover the div

Why does jQuery .css('background-color') return rgba(0,0,0,0) for 'transparent'?

Player1 control keys work, player2 keys don't work in pygame, any fixes?

Open CV - iOS PNG Overlaying images with transparent background shows white background

If num1 or num2 don‘t equal 0

JLabels have transparent background on Mac, but on windows they don't?

Don't understand why (5 | -2) > 0 is False where (5 or -2) > 0 is True

Export plot in .png with transparent background

set opacity of all background images to 0 with plugin

Placeholder images don't work in Firefox

Literals (Images & Colors) don't work in Xcode

Webgl Cross Origin Images don't work

Why CSS style don't work with the images?

Background Service don't work - Android

RxSwift TestScheduler don't work if subscribeOn background

i'm trying to make a 2 players racing game with pygame and event.key don't work

NVidia drivers don't work properly: `Failed to load module "nvidia" (module doesn't support this OS, 0)`

Why my code doesn't work if i don't put case 0 in switch

ImageButton with PNG image with transparent background as background

Setting transparent images background in IrfanView