How to make the background of a pygame sprite transparent - python

This is supposed to just load a sprite onto the screen, but the background of the image is this black box, and i don't want to see that. How do I make the background of the image transparent?
from Tkinter import *
import pygame
from livewires import games
#Creating screen window
games.init(screen_width = 700, screen_height = 650, fps = 50)
#creating background image for the screen
screen_background = games.load_image('resized_stars background.png', transparent = False)
games.screen.background = screen_background
#Creating a sprite
spaceship_image = games.load_image('pixel_spaceship.png')
spaceship = games.Sprite(image = spaceship_image, x=350, y=235)
games.screen.add(spaceship)
#main loop at the end of the program just as in tkinter
games.screen.mainloop()

Your image needs to be a png. Open Preview, for example, and use the alpha tool to remove the background, thus adding transparency.
Before png:
After png:
Look here for a tool on making your picture transparent.

When loading the png image, you have to convert it:
image = pygame.image.load("yourImage.png").convert_alpha()
This should get rid of that black box that should be the transparent alpha.

Related

How can I use SVGs with pygame (or perhaps display PNGs at a higher definition)?

I'm trying to make a chess opening trainer with Pygame. Graphically, it's incredibly simple; it works like a slideshow, I am only blitting a single image to the screen at a time which updates when I press the return key. I am using chess.svg to generate an SVG of the current game state. Not being able to use SVGs natively with pygame means that I have been converting the SVG to a PNG.
Viewing the PNG on my computer, it's very clear.
However when pygame displays the image, it's very blurry.
Ideally I would like pygame to display the SVG so I don't have to worry about the resolution at all. All I found on pygame and SVGs was this link from 12 years ago. In 2018, an answer was posted stating that the original methods no longer worked. However, now, in 2021 the methods given in 2018 no longer work either; I tried, and one comment also confirms this.
If using SVGs is not possible, that's fine, provided I can get pygame to display the PNG at a greater definition than it currently does.
Even if I make screen = pygame.display.set_mode((width, height)) fullscreen, it's still blurry. I can't seem to find anything on that, so I'm not sure that's the issue here.
Here's a simplified version of my code to demonstrate the graphical part:
import pygame
from pygame.locals import *
import chess
import cairosvg
import chess.svg
import chess.pgn
##Setup Pygame:
pygame.init()
width, height = 640, 640
screen = pygame.display.set_mode((width, height))
## MAIN ##
board = chess.Board()
#Step 1: Create image
boardsvg = chess.svg.board(board=board)
f = open("image.SVG", "w")
f.write(boardsvg)
f.close()
#scale
cairosvg.svg2png(url="image.svg", write_to="image_scaled.png", scale=3.0)
#Step2: Blit the image
image = pygame.image.load('image_scaled.png')
image = pygame.transform.scale(image, (640, 640))
screen.blit(image,(0,0))
pygame.display.flip()
Does anyone one know what I need to try?
Cheers.
I found that using smoothscale like so image = pygame.transform.smoothscale(image, (width, height)) helped a little, but it still wasn't perfect. In the end I decided to rewrite it in javascript using cm-chessboard.
chess.svg.board has a size keyword argument:
size – The size of the image in pixels (e.g., 400 for a 400 by 400 board), or None (the default) for no size limit.
e.g.
boardsvg = chess.svg.board(board=board, size=1024)
Setting this to your output size should fix the blurryness.
In 2021 you can use Pygame 2 + SDL2, which loads SVG images directly, no need of cairo or any other lib:
import io
import pygame
from pygame.locals import *
import chess
import chess.svg
##Setup Pygame:
pygame.init()
width, height = 640, 640
screen = pygame.display.set_mode((width, height))
## MAIN ##
board = chess.Board()
#Step 1: Create image
boardsvg = io.BytesIO(chess.svg.board(board=board).encode())
#Step 2: Blit the image
image = pygame.image.load(boardsvg)
image = pygame.transform.scale(image, (640, 640))
screen.blit(image,(0,0))
pygame.display.flip()

Raspberry Pi not showing pygame with screen as expected

I am trying to get a Raspberry Pi 3B+ to show live streams from 3 cameras, using a screen to get multiple instances of omxplayer to run on the positions I want them. This works exactly as expected.
The problem now is, that I want a static image in the last space on the screen. For that I wanted to use pygame, so I later could have more fun in that area, and maybe not only have an image.
The problem now is how the image is positioned:
https://imgur.com/mUQ38vV (the image is the same size as the video feeds, and I had expected it to be in the bottom right of the monitor, with a thin black line towards the feed above and to the right of it)
I expected the white square to be directly under the top left video feed, but there are large black borders.
The Python code I use for showing the image is as follows:
import time
import pygame
transform_x = 958 #648 #how wide to scale the jpg when replaying
transfrom_y = 539 #how high to scale the jpg when replaying
offset_x = 0 #how far off to left corner to display photos
offset_y = 540 #how far off to left corner to display photos
try:
pygame.init()
info = pygame.display.Info() # You have to call this before pygame.display.set_mode()
screen_width,screen_height = info.current_w,info.current_h
window_width,window_height = screen_width,screen_height
screen = pygame.display.set_mode((window_width, window_height))
#screen = pygame.display.set_mode((0,0))
pygame.mouse.set_visible(False) #hide the mouse cursor
filename = "image.png"
img=pygame.image.load(filename)
#img = pygame.transform.scale(img,(transform_x,transfrom_y))
screen.blit(img,(offset_x,offset_y))
pygame.display.flip() # update the display
time.sleep(30) # pause
finally:
pygame.quit()
I tried with pygame.FULLSCREEN and other modes, but none of them would go all the way to the edge of the monitor.
To show the picture I used this command:
sudo screen -dmS pic sh -c 'python pic.py'
Can anyone help me figure out what I am doing wrong here?
So I got it fixed...
I had to comment out disable_overscan=1 in /boot/config.txt ...
I didn't think of this, because omxplayer was at the positions I expected it to be..

Take screenshot then blit it

Is there a way to take a screenshot (a copy of the previous frame blitted) with pygame from the GUI, assign it to a surface variable and blit it? Without saving the screenshot to a file, then loading it, and finally blitting it,
import pygame, os
pygame.init()
screen = pygame.display.set_mode((800, 400))
def TakeScreenShot(screen):
pygame.image.save(screen, 'ScreenShot.png')
pic = pygame.image.load(os.path.join('ScreenShot.png')).convert()
return pic
pic = TakeScreenShot(screen)
screen.blit(pic, [0,0])
pygame.display.flip()
The saving screenshot then loading seems really unneccsary...is there a method to bypass this and directly blit the screenshot taken to the GUI?
I.e., something such as,
import pygame, os
pygame.init()
screen = pygame.display.set_mode((800, 400))
pic = screen.getLastFrameBlitted()
screen.blit(pic, [0,0])
pygame.display.flip()
You can take a copy of the screen anytime you want and save it in a variable.
screenshot = screen.copy()
screen is just a regular Surface object and can be treated as such. Blitting the screenshot is done as usual
screen.blit(screenshot, (0, 0))

How to draw an image in (pygame)

I want to replace this draw rectangle function, and instead let it produce an image.
Rectangle Function:
pygame.draw.rect(pygame.display.set_mode((800,600)), black, [0,0, 100, 100])
Looking for something on the lines of this:
pygame.draw.image(**coordinatesX**, **coordinatesY**, **imagefile.jpg**)
To load an image, you need the file stored, and you load it using:
pygame.image.load('example.png')
Now your code should look like:
pygame.image.load('example.png')
pygame.display.set_mode((800, 600))
so first you have to load the image using image = pygame.image.load() and then use screen.blit(image) to draw it on the screen. For this to work you should define the screen at the beginning not in the draw function like you have done above. Just screen = pygame.display.set_mode((800,600)) should work.

Python imaging, resize Turtle Graphics window

My image is too large for the turtle window. I had to enlarge the image because the text I need at each spot overlaps.
How do I Resize the window in python?
It sounds like you have drawn an image, but it has gone outside the borders of the window, so therefore you need to make the window larger to see the entire image.
To resize the window:
setup( width = 200, height = 200, startx = None, starty = None)
This will make your output window 200X200 (which may be too small for you so you'll need to make those numbers larger.)
Here is the URL where I found this information.
TurtleDocs

Categories