Using Surface.copy() sometimes loses transparency - python

For some (but not all!) images, copying a surface using surface.copy() loses the transparency. So I've got two questions?
Why does copy lose the transparency? The docs sound like everything about the new surface should be the same, but that's obviously not happening.
Why does this happen with some images and not others?
Here is an example "bad" image -- when copied, the transparency is lost
Here is an example "good" image -- when copied, the transparency is not lost.
And here is the code that you can run to see the difference:
import pygame
def test():
screen = pygame.display.set_mode((320, 240))
bad_original = pygame.image.load('bad-image.gif')
bad_copied = bad_original.copy()
good_original = pygame.image.load('good-image.gif')
good_copied = good_original.copy()
while True:
for event in pygame.event.get():
if (event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE or
event.type == pygame.QUIT):
pygame.quit()
screen.fill((150, 150, 150))
screen.blit(bad_original, (0,0))
screen.blit(bad_copied, (100, 0))
screen.blit(good_original, (0,100))
screen.blit(good_copied, (100, 100))
pygame.display.flip()
if __name__ == '__main__':
test()
And heck, for completion, here's what a screenshot of running the above code looks like.
Please note that I'm not looking for workarounds; I just want to know what I am not understanding about surface.copy, or anything you think I may not understand about working with Pygame surfaces.
I'm using Python 3.3 and Pygame 1.9.2pre on a Windows 7 machine.

You need to use .convert_alpha()
Try:
pygame.image.load('my_image.gif').convert_alpha()
See:
http://www.pygame.org/docs/ref/surface.html#pygame.Surface.convert_alpha
"Creates a new copy of the surface with the desired pixel format. The new surface will be in a format suited for quick blitting to the given format with per pixel alpha. If no surface is given, the new surface will be optimized for blitting to the current display.
Unlike the Surface.convert() method, the pixel format for the new image will not be exactly the same as the requested source, but it will be optimized for fast alpha blitting to the destination."
In pygame anytime you load and image, or create a surface, with the intent of displaying it you should .convert() it if it has no transparency, or .convert_alpha() it if it has transparency. This yields both a big speedup AND solves the mystery of, 'Why is my transparency doing that?'.

Related

image not displaying nicely in pygame

i am trying to display this image in a fullscreen window in pygame as the texture for my floor
but when i try to actually display the image inside the pygame window, it comes up as this
for some reason there are random blobs of blue coming up in the image. i used this code to try and put in my image
width,height=pygame.display.get_surface().get_size()
floorimg=pygame.image.load("assets//image.png")
win.blit(floorimg,(0,height-100))
how can i fix this?
Seems like the problem is not the code but the image itself...
Attached is the link of the "floor" image with the transparent area colored using Windows Paint3D.
The code works perfectly fine, but you might need to get some clean pictures in the future.
I saved your image and wrote this script to draw it:
import pygame, sys
pygame.init()
window = pygame.display.set_mode((1300, 200), 0 , 32)
width,height=pygame.display.get_surface().get_size()
floorimg = pygame.image.load("floor.png")
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
window.fill((0))
window.blit(floorimg,(0,height-100))
pygame.display.flip()
It seems to have similar problem to yours if the image is converted to pygame format(which I am assuming you do somewhere else in your code?)
floorimg = pygame.image.load("floor.png").convert()
But this can easily be solved by taking transparency into account while converting, which is done by convert_alpha.
floorimg = pygame.image.load("floor.png").convert_alpha()
This results in an image similar to the first one.

How to use pygame.surface.scroll()?

I just found out about pygame.surface.scroll() and what I understand from the pygame documents that scroll() is for moving surface without the need to rebuild the background again to cover the old surface, just like pygame.rect.move_ip() but for surfaces.
Anyway, I don't know how to use it and the examples in the pygame documents are hard to me to understand as long as I am beginner and, after searching for long time, I couldn't found anything useful to understand how to use it.
Here is my code.
import pygame
from pygame.locals import*
screen=pygame.display.set_mode((1250,720))
pygame.init()
clock=pygame.time.Clock()
boxx=200
boxy=200
image = pygame.Surface([20,20]).convert_alpha()
image.fill((255,255,255))
while True :
screen.fill((0,0,0))
for event in pygame.event.get():
if event.type==pygame.QUIT :
pygame.quit()
quit()
image.scroll(10,10)
screen.blit(image,(boxx,boxy))
pygame.display.update()
clock.tick(60)
EDIT: Your image and screen variables are backwards. That is also causing you some confusion I'm sure..
Your problem may is that you are trying to scroll an all black background. It is probably scrolling, and you just don't know it because the white box you used blit() to draw on the screen is stationary.
Try using something you can see scroll, like an image file. If you wanna move the white box, you can add a counter as a speed variable. Read this, then run it.
import pygame
from pygame.locals import*
screen=pygame.display.set_mode((1250,720))
pygame.init()
clock=pygame.time.Clock()
boxx=200
boxy=200
image = pygame.Surface([20,20]).convert_alpha()
image.fill((255,255,255))
speed = 5 # larger values will move objects faster
while True :
screen.fill((0,0,0))
for event in pygame.event.get():
if event.type==pygame.QUIT :
pygame.quit()
quit()
image.scroll(10,10)
# I did modulus 720, the surface width, so it doesn't go off screen
screen.blit(image,((boxx + speed) % 720, (boxy + speed) % 720))
pygame.display.update()
clock.tick(60)
I can't say for sure the scroll function is working or not, learn to use an image as your background so you can see it moving first.

Force a fullscreen resolution that is not contained in display.list_modes()

I'm making a simulation for school and I'm trying to make pygame create a fullscreen display in my native resolution. However, I have a QHD screen (2560x1440), and it isn't working properly. As far as I can tell, pygame is rendering a screen at the correct resolution, but expanding it so it is scaled as if it were 1080p, so about 300-400 pixels are cut off around the edges. This causes, for example, a circle rendered at (200,200) to be completely invisible. After some research, I learned that this is because pygame doesn't officially support my resolution (it is not listed in pygame.display.list_modes()). Is there any way to force it to work? I would prefer if I could use my actual resolution instead of upscaled 1080p.
Here is the code that initializes the window:
import pygame
from pygame.locals import *
pygame.init()
w = pygame.display.Info().current_w
h = pygame.display.Info().current_h
S = pygame.display.set_mode((w,h), pygame.FULLSCREEN)
If you are using Windows, make sure in your display settings you have scaling set to 100%. This will make your text and everything smaller if you don't have it at that currently but I think Pygame windows get affected by this number for some reason.
See the below code snippet for making sure your window scales properly. Also see here.
import pygame
from ctypes import windll
def run():
# Force windows to ignore the display scaling value.
windll.user32.SetProcessDPIAware()
pygame.display.init()
# Make the screen the highest resolution that will fit the screen at 100% scaling.
screen = pygame.display.set_mode(pygame.display.list_modes()[0])
done = False
while not done:
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
done = True
pygame.display.flip()
pygame.quit()
if __name__ == "__main__":
run()

Make the background of an image transparent in Pygame with convert_alpha

I am trying to make the background of an image transparent in a Pygame script. Now the background in my game is black, instead of transparent. I read somewhere else that I could use convert_alpha, but it doesn't seem to work.
Here is (the relevant part of) my code:
import PIL
gameDisplay = pygame.display.set_mode((display_width, display_height))
img = pygame.image.load('snakehead1.bmp').convert_alpha(gameDisplay)
What am I doing wrong? Thanks in advance!
To make an image transparent you first need an image with alpha values. Be sure that it meets this criteria! I noticed that my images doesn't save the alpha values when saving as bmp. Apparently, the bmp format do not have native transparency support.
If it does contain alpha you should be able to use the method convert_alpha() to return a Surface with per-pixel alpha. You don't need to pass anything to this method; if no arguments are given the new surface will be optimized for blitting to the current display.
Here's an example code demonstrating this:
import pygame
pygame.init()
screen = pygame.display.set_mode((100, 100))
image = pygame.image.load("temp.png").convert_alpha()
while True:
screen.fill((255, 255, 255))
for event in pygame.event.get():
if event.type == pygame.QUIT:
quit()
screen.blit(image, (0, 0))
pygame.display.update()
And my image ("temp.png"):
If it doesn't contain alpha there are two easy fixes.
Save the image with a different file format, like png.
Use colorkeys. This works if you only need to remove the background. It is as easy as putting the line of code image.set_colorkey((0, 0, 0)), which will make all black colors transparent.

How to get rid of pygame surfaces?

In the following code, there is not just one circle on the screen at any given point in time.
I want to fix this to make it so that it looks like there is only one circle, instead of leaving a smudge trail where ever the mouse cursor has been.
import pygame,sys
from pygame.locals import *
pygame.init()
screen = pygame.display.set_mode((640,400),0,32)
radius = 25
circle = pygame.Surface([radius*2]*2,SRCALPHA,32)
circle = circle.convert_alpha()
pygame.draw.circle(circle,(25,46,100),[radius]*2,radius)
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
screen.blit(circle,(pygame.mouse.get_pos()[0],100))
pygame.display.update()
pygame.time.delay(10)
You need to specifically erase the circle before you blit it again. Depending on how complicated your scene is, you may have to try different methods. Generally what I do is have a "background" surface that a blit to the screen every frame and then blit the sprites/other surfaces in their new positions (blits in Pygame are very fast, so even in fairly large screens I haven't had speed issues doing this). For your code above, it's simple enough just to use surface.fill(COLOR) where COLOR is your background color; eg, (255,255,255) for white:
# ...
screen = pygame.display.set_mode((640,400),0,32)
backgroundColor = (255,255,255)
# ...
while True:
# ...
screen.fill(backgroundColor)
screen.blit(circle,(pygame.mouse.get_pos()[0],100))
pygame.display.update()
pygame.time.delay(10)
Edit in answer to your comment: It is possible to do this in a more object-oriented way.
You will need to have a background Surface associated with your screen (I usually have a Display or Map class (depending on the type of game) that does this). Then, make your object a subclass of pygame.sprite. This requires that you have self.image and self.rect attributes (the image being your surface and the rect being a Pygame.rect with the location). Add all of your sprites to a pygame.group object. Now, every frame, you call the draw method on the group and, after you update the display (ie, with pygame.display.update()), you call the clear method on the group. This method requires that you provide both the destination surface (ie, screen above) and a background image.
For example, your main loop may look more like this:
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
circle.rect.center = (pygame.mouse.get_pos()[0],100)
circleGroup.draw(screen)
pygame.display.update()
circleGroup.clear(screen, backgroundSurface)
pygame.time.delay(10)
See the documentation on the Sprite and Group classes for more information.

Categories