I have a selection of images that I will build a backdrop to a game from (it's a TD game but each level will be built from walls, walkways etc). I'm trying to work out how to dynamically build a single image to use as a background rather than having 100+ sprites for a background.
you will have to make pygame.surface with your images.
images = []
for image_name in image_names:
images.append(pygame.image.load(image_name))
background = pygame.display.get_surface()
for image in images:
back.blit(image, image_position) # how you compute the image position is your stuff ;)
pygame.display.update()
You can always use PIL
See this post for details about how to combine images. You can basically "paste" one image into another image. Make one big blank image for your background, and then paste all the sprites into the background image.
Related
I want to create a mask over the whole human figure and replace it with an image that is responsive to the moving human figure. The image attached will give you a better idea of what is I mean.
The Image in the background will be still and won't move but the figure will seem like it punched a hole into the foreground. The figures however they move will go over the static background image and show the contents of the image. The figures, if they overlap as seen in the first image (see overlapping leg) should not break the mask and should still display the contents of the background.
I am trying to make a game in python 3 which requires a background image. I already have the image. The problem here is my image looks small compared to the big screen. I don't want to change the size of the screen because then I would have to redo all the coordinates of the other objects on the screen. Is there any way I can increase the size of the background image?
Ps. I'm using Python 3 and on VS code.
Thanks in advance!
This is the picture of what the small picture looks like.enter image description here
In order to increase the size of the image, you would need too increase the resolution.
Assuming that you are using windows, open the png with Microsoft Photos, and click on the three horizontal dots at the top right.
A dropdown menu will open, press the third option from the top labeled "Resize"
After this, press on "Define custom dimensions" and you may manipulate the dimensions of the image as you like.
Then, simply save the resized image, and use it in your project.
you can also do this using CV2 library
import cv2
image=cv2.imread("image.png")
scale_percent=1.5
width=int(image.shape[1]*scale_percent)
height=int(image.shape[0]*scale_percent)
dimension=(width,height)
resized = cv2.resize(image,dimension, interpolation = cv2.INTER_AREA)
print(resized.shape)
cv2.imwrite("output.png",resized)
OR you can use PIL library as well
from PIL import Image
image = Image.open("image.png")
image.save("output.png", dpi=(image.size[0]*1.5,image.size[1]*1.5))
I want to blit only green triangle of this image on pygame surface.
My code:
import pygame
win = pygame.display.set_mode((800,800))
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
win.fill((255,255,255))
Image = pygame.image.load('sample.jpg').convert()
Image.set_colorkey((0,0,0))
win.blit(Image,(80,80))
pygame.display.update()
pygame.quit()
When I run that code it blit image like this.
How can I do this?
If you load your original triangle bitmap into a graphics editor like The GIMP, selecting the colours in the area around your triangle giving problems shows that these colours are not exactly black, but very very dark grey, e.g. ( 0, 0, 3 ). This is probably because the image is saved as a JPEG, where there is non-pixel-perfect image storage, and the colours have become corrupt.
Try this re-draw image, a .PNG - which does have pixel-perfect compression:
sample.png
But if you're going to the trouble, you may as well just make the unwanted sections transparent with an alpha channel, then PyGame will hide the background automatically.
Unfortunately, pygame doesn't do a good job in color filtering. Instead you can try to blit a png image which has alpha transparency built in and you can use tools such as GIMP or photoshop to make the transparent image.
But if you have a lot of images and you want to do the process automatically you can use opencv to filter colors seamlessly; here is a good tutorial on this:
https://realpython.com/python-opencv-color-spaces/
After filtering the specified color, You can also use morphological operations to achieve a better result:
https://docs.opencv.org/trunk/d9/d61/tutorial_py_morphological_ops.html
When using python and pygame: after loading the screen with the background image and blitting new objects (Text, circles, rectangles, etc.), is there a way to save the modified screen so as to be recalled later in the program? Specifically, I am setting the background and blitting new objects and would like to save the screen image with all of the blits in intact so it can be used later in the program as a new background upon which sprites can be manipulated. Any suggestions welcomed!
Blitting works both ways, meaning you can blit something onto the display screen, but you can also blit the screen onto another surface. So simply make a new surface the same size of your display surface and blit the screen onto that surface for later use.
found a solution and it works better than I expected:
after blitted my raw background onto my surface and then adding numerous circles, rectangles and text to make an image with multiple dial, gauges and labels I ran the following:
pygame.display.update()
window = pygame.display.set_mode((800,480),0,32)
pygame.image.save(TFT,"screen_update.jpg")
the new image is saved to disk(XDcard on my RPi2) as "screen_update.jpg"
then I simply change the name to "ANAL_update.jpg" and use that as the background on my next program run. I commented out all of the code used to create the rectangles, circles and labels and it works. I will add an selectable "update" routine to the program and move all of extra drawing and labelling to that routine to be used when I wish to change the layout of he background. I like the fact that the program creates a new updated file that just needs to be renamed for use and for copying the background to other machines.
note: This is working on my RaspberryPi 2B with HDMI output to a 42" HD tv for development, but it is intended to run on an RPi3B with he official RPi 7 inch TFT display. Thanks to all of you that responded and to the others who left pertinent code for previous questions similar to mine.
I want to make something that you can compare images and choose the one you like the best in pygame, how can I display multiple images in one screen?
Load a second image and reposition it next to the first image object.
Here is a tip on how to work with positioning: Pygame Image position