Python Forum
display error, png with transparent background
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
display error, png with transparent background
#1
Dear community,

I display a paddle and a ball in a breakout game.

I've got an error concerning the display of the paddle.

Both pictures (ball + paddle) have got a transparent background.

The ball is displayed correctely - the paddle not (it has a white rectangle as background).

Online I read that I have to you use "convert_alpha()" to display pictures with a transparent background...

I attached ball.png and paddle.png.
In addition to that I attached the wrong display.

Would you please have a look at these files and perhaps tell me what's wrong with paddle.png?
def create_image(file, color=None):
    """
    Create image from a file.  If color is specified, replace all FOREGROUND
    pixels with color pixels.  Modify image so TRANSPARENT colored pixels are
    transparent.
    """
    if color:
        # Recolor the image
        image = Image.open(file).convert("RGB")
        for xy in product(range(image.width), range(image.height)):
            if image.getpixel(xy) == FOREGROUND:
                image.putpixel(xy, color)
        image = pygame.image.fromstring(image.tobytes(), image.size, "RGB")
    else:
        image = pygame.image.load(file)
    image.set_colorkey(TRANSPARENT)
    return image.convert_alpha()

Attached Files

Thumbnail(s)
       
Image(s)
   
Reply
#2
Have a look at this post
https://stackoverflow.com/questions/3993...vert-alpha
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#3
This works as expected on my system. Will you please post enough code to produce the problem?
import pygame
pygame.init ()
SCREEN = pygame.display.set_mode ((300, 100))
TRANSPARENT = (0, 0, 0)

def create_image(file, color=None):
	"""
	Create image from a file.  If color is specified, replace all FOREGROUND
	pixels with color pixels.  Modify image so TRANSPARENT colored pixels are
	transparent.
	"""
	if color:
		# Recolor the image
		image = Image.open(file).convert("RGB")
		for xy in product(range(image.width), range(image.height)):
			if image.getpixel(xy) == FOREGROUND:
				image.putpixel(xy, color)
		image = pygame.image.fromstring(image.tobytes(), image.size, "RGB")
	else:
		image = pygame.image.load(file)
	image.set_colorkey(TRANSPARENT)
	return image.convert_alpha()

paddle = create_image ('paddle.png')
while True :
	for event in pygame.event.get () :
		if event.type == pygame.QUIT :
			pygame.quit ()
			quit ()

	SCREEN.fill ((0, 0, 0))
	SCREEN.blit (paddle, (70, 40))
	pygame.display.update ()
metulburr and menator01 like this post

Attached Files

Thumbnail(s)
   
Reply
#4
Works fine for me too. I modified my create_image to match, loaded you paddle image and no problems. When you make the paddle image are you passing a color argument? The color should be None. If color is not None the image is converted to RGB (not RGBA) and it replaces FOREGROUND colored pixels with color. Even if there are no matches the conversion to RGBA removes the transparent background.
Reply
#5
Dear community,
thanks a lot for your effort!!
I don't need help concerning the transparent background problem anymore.
Many thanks again...
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [pygame] transparent rects SheeppOSU 2 2,303 Jun-10-2019, 03:41 PM
Last Post: SheeppOSU

Forum Jump:

User Panel Messages

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