I have a pretty weird situation but i will try hard to explain everything the best i can. So im coding a game using PyGame. I used moviepy to show a video on the surface. Now im doing a combatscreen and i want to animate the attacks. Ill show a quick screenshot
When clicked on "Fireball" i want to show a fireball. The video itself has transparency but it fills it up with white.
The code i used previously to show cutscenes for example is following:
video = VideoFileClip('assets/Attacks/Frantz/fireball.webm')
video.preview()
When it plays the video it looks like this:
My initial file was a gif that i converted to an mp4. i found out that mp4 doesn't support alpha/transparency i tried using the gif by replacing video = VideoFileClip('assets/Attacks/Frantz/fireball.mp4') with video = VideoFileClip('assets/Attacks/Frantz/fireball.gif')
but the same thing happend with the white background (And yes the gif has 100% transparency)
I kinda don't know what to do. Should i try other file formats, if yes how do i remove the transparency but i think i need to change something in the code so i might be able to actually use a gif or something.
Heres the file of the gif btw
I know my issue if very weird but its for a school project and i would greatly appreciate some help
A movie is not what you want. What you want is an animated sprite. An animated sprite is made up of many different sprites that are displayed in consecutive frames. The source of these sprites can be a sprite sheet, an animated GIF, or a list of bitmaps.
There are various questions and answers on this topic. For example:
Animated sprite from few images
How can I load an animated GIF and get all of the individual frames in PyGame?
How do I create animated sprites using Sprite Sheets in Pygame?
Since your GIF is not transparent, you have to set the color key for the transparent color with set_colorkey():
pygameImage.set_colorkey((0, 0, 0))
Example:
import pygame
from PIL import Image, ImageSequence
def pilImageToSurface(pilImage):
return pygame.image.fromstring(
pilImage.tobytes(), pilImage.size, pilImage.mode).convert()
def pilImageToSurface(pilImage):
return pygame.image.fromstring(
pilImage.tobytes(), pilImage.size, pilImage.mode).convert()
def loadGIF(filename):
pilImage = Image.open(filename)
frames = []
if pilImage.format == 'GIF' and pilImage.is_animated:
for frame in ImageSequence.Iterator(pilImage):
pygameImage = pilImageToSurface(frame.convert('RGB'))
pygameImage.set_colorkey((0, 0, 0))
frames.append(pygameImage)
else:
frames.append(pilImageToSurface(pilImage))
return frames
pygame.init()
window = pygame.display.set_mode((500, 500))
clock = pygame.time.Clock()
background = pygame.Surface(window.get_size())
ts, w, h, c1, c2 = 50, *window.get_size(), (128, 128, 128), (64, 64, 64)
tiles = [((x*ts, y*ts, ts, ts), c1 if (x+y) % 2 == 0 else c2) for x in range((w+ts-1)//ts) for y in range((h+ts-1)//ts)]
for rect, color in tiles:#
pygame.draw.rect(background, color, rect)
gifFrameList = loadGIF('fire.gif')
currentFrame = 0
run = True
while run:
clock.tick(20)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
window.blit(background, (0, 0))
rect = gifFrameList[currentFrame].get_rect(center = (250, 250))
window.blit(gifFrameList[currentFrame], rect)
currentFrame = (currentFrame + 1) % len(gifFrameList)
pygame.display.flip()
pygame.quit()
exit()
Related
I had opened an image using PIL, as
image = Image.open("SomeImage.png")
Draw some text on it, as
draw = ImageDraw.Draw(image)
draw.text(Some parameters here)
and then saved it as
image.save("SomeOtherName.png")
to open it using pygame.image
this_image = pygame.image.load("SomeOtherName.png")
I just want to do it without saving.. Can that be possible? It is taking a lot of time to save and then load(0.12 sec Yes, that is more as I have multiple images which require this operation). Can that save method be surpassed?
Sadly the accepted answer doesn't work anymore, because Image.tostring() has been removed. It has been replaced by Image.tobytes(). See Pillow - Image Module.
Function to convert a PIL Image to a pygame.Surface object:
def pilImageToSurface(pilImage):
return pygame.image.fromstring(
pilImage.tobytes(), pilImage.size, pilImage.mode).convert()
It is recommended to convert() the Surface to have the same pixel format as the display Surface.
Minimal example:
import pygame
from PIL import Image
def pilImageToSurface(pilImage):
return pygame.image.fromstring(
pilImage.tobytes(), pilImage.size, pilImage.mode).convert()
pygame.init()
window = pygame.display.set_mode((500, 500))
clock = pygame.time.Clock()
pilImage = Image.open('myimage.png')
pygameSurface = pilImageToSurface(pilImage)
run = True
while run:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
window.fill(0)
window.blit(pygameSurface, pygameSurface.get_rect(center = (250, 250)))
pygame.display.flip()
You could use the fromstring() function from pygame.image. The following should work, according to the documentation:
image = Image.open("SomeImage.png")
draw = ImageDraw.Draw(image)
draw.text(Some parameters here)
mode = image.mode
size = image.size
data = image.tostring()
this_image = pygame.image.fromstring(data, size, mode)
This question already has answers here:
How can I load an animated GIF and get all of the individual frames in Pygame?
(3 answers)
Closed 2 years ago.
So I have this idea for a game where you dodge projectiles while controlling a helicopter. I am wondering if you can make a sprite appear as a gif, or something along the lines of two images switching every fraction of a second. I know how to make a sprite appear as one image:
self.surf = pygame.image.load("example.png").convert()
But I was wondering if this had an effect:
self.surf = pygame.image.load("example.gif").convert()
Unfortunately, it only displayed the first image in the gif.
Here is the gif:
Edit: Ok so I looked at the answers and tried to implement them in my code, but then it was all too confusing and I had tried to do something a bit more simple. This is what I came up with:
if play == 1:
self.surf = pygame.image.load("Image2.png").convert()
pygame.display.update()
play = 2
time.sleep(.2)
if play == 2:
self.surf = pygame.image.load("Image1.png").convert()
pygame.display.update()
play = 1
time.sleep(.2)
But, all that did was display the player sprite as image 1. Is there anything I can add to make this work?
PyGame respectively the pygame.image module can only handle non-animated GIFs.
But on the PyGame homepage is introduced the GIFImage library:
This library adds GIF animation playback to pygame.
Another option is to use the Pillow library (pip install Pillow).
Write a function, that can convert a PIL image to a pygame.Surface:
(see also PIL and pygame.image)
def pilImageToSurface(pilImage):
mode, size, data = pilImage.mode, pilImage.size, pilImage.tobytes()
return pygame.image.fromstring(data, size, mode).convert_alpha()
Use the PIL library to load a GIF frame by frame:
(see also Extracting The Frames Of An Animated GIF Using Pillow
def loadGIF(filename):
pilImage = Image.open(filename)
frames = []
if pilImage.format == 'GIF' and pilImage.is_animated:
for frame in ImageSequence.Iterator(pilImage):
pygameImage = pilImageToSurface(frame.convert('RGBA'))
frames.append(pygameImage)
else:
frames.append(pilImageToSurface(pilImage))
return frames
See also Load animated GIF and a simple animated gif viewer example:
import pygame
from PIL import Image
def pilImageToSurface(pilImage):
mode, size, data = pilImage.mode, pilImage.size, pilImage.tobytes()
return pygame.image.fromstring(data, size, mode).convert_alpha()
def loadGIF(filename):
pilImage = Image.open(filename)
frames = []
if pilImage.format == 'GIF' and pilImage.is_animated:
for frame in ImageSequence.Iterator(pilImage):
pygameImage = pilImageToSurface(frame.convert('RGBA'))
frames.append(pygameImage)
else:
frames.append(pilImageToSurface(pilImage))
return frames
pygame.init()
window = pygame.display.set_mode((500, 500))
clock = pygame.time.Clock()
gifFrameList = loadGIF("my_gif.gif")
currentFrame = 0
run = True
while run:
clock.tick(20)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
window.fill(0)
rect = gifFrameList[currentFrame].get_rect(center = (250, 250))
window.blit(gifFrameList[currentFrame], rect)
currentFrame = (currentFrame + 1) % len(gifFrameList)
pygame.display.flip()
You can use the list of pygame.Surface objects to generate a Spritesheet.
Pygame can't do gifs, but if you really want to, you could animate it frame by frame one image at a time.
i need to put a university uniform to a camera feed to place their faces on top of it and take a snapshot and upload it to our database. i need some help in my code written below.
1st problem: I cant call my webcam
2nd problem: my overlaying image isnt appearing right as i want it to be.(i need it to the bottom of the window...)
I am following this youtube tutorial.
import pygame, sys
import pygame.camera
from pygame.locals import *
pygame.init()
pygame.camera.init()
screen = pygame.display.set_mode((640, 480))
cam = pygame.camera.Camera("/dev/video0", (640,480))
cam.start()
background=pygame.Surface((window.get_rect().width,
window.get_rect().height))
background.fill((0, 0, 0))
image=pygame.image.load('College Boy Medium.png')
image=image.convert()
image = pygame.transform.scale(image, (720,640))
rect=image.get_rect()
while 1:
image = cam.get_image()
screen.blit(image,(0,0))
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit
Screen.blit(image,(0,0)) the 0,0 refers to the upper left of the image box x y position.
So 0,20 would place the image at x position 0 and the y position down 20 pixels.
I'm trying to make a user interface thing transparent in my game when the mouse isn't hovering over it. But for some reason, when I set the alpha value of the image for it to become transparent, nothing happens. Here is some runnable code for it that replicates the problem:
import pygame
WHITE = (255, 255, 255)
class UI:
def __init__(self):
self.img = pygame.image.load("ink_bar_solid.png")
self.img.set_alpha(0)
self.ink_bar_rect = self.img.get_bounding_rect()
self.x, self.y = 0, 10
resolution = (500, 500)
screen = pygame.display.set_mode(resolution)
mouse = pygame.mouse.get_pos
ink_bar = UI()
run = True
def mouse_over():
if ink_bar.ink_bar_rect.collidepoint(mouse()):
ink_bar.img.set_alpha(255)
else:
ink_bar.img.set_alpha(0)
while run:
mouse_over()
screen.fill(WHITE)
screen.blit(ink_bar.img, (ink_bar.x, ink_bar.y))
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
break
pygame.display.flip()
pygame.quit()
Any help is greatly appreciated!
Edit: I got a comment from someone who said they used their own image and it worked fine... I'm getting this warning when I execute the program:
libpng warning: iCCP: known incorrect sRGB profile
Is the reason why it doesn't blit properly because of my file?
The set_alpha method doesn't seem to work for unconverted png files. Calling the convert method will also improve the blit performance drastically:
self.img = pygame.image.load("ink_bar_solid.png").convert()
It also doesn't work for per-pixel alpha surfaces (surfaces converted with convert_alpha or created with the pygame.SRCALPHA flag). The alpha of per-pixel surfaces can be changed by filling them with a transparent white color and passing the pygame.BLEND_RGBA_MULT special flag, e.g.:
image = pygame.image.load('an_image.png').convert_alpha()
# Make a copy so that the original doesn't get modified.
transparent_image = image.copy()
transparent_image.fill((255, 255, 255, 100), special_flags=pygame.BLEND_RGBA_MULT)
Is it possible to stretch an image in pygame using an event to trigger it?
Like say I have a person and I want his eyes to popout like this
when I press a button and I am using surface.blit(eyes=pygame.image.load('eyes')) for the eyes.
Can i stretch the eye image like the picture in this link?
There is a solution to this problem that allows you to stretch the eyes to the exact width you want, but it may make the eyes very deformed... (nevermind, the original image has pretty deformed eyes anyway.)
From the Pygame doc on pygame.transform.scale:
scale(Surface, (width, height), DestSurface = None) -> Surface
We also use image.get_height() so the user does not have to get the height themselves.
So you would do something like this (wrapped in a function):
def stretchEyes(image, newWidth):
return pygame.transform.scale(image, (image.get_height(), newWidth))
eyes = stretchEyes(eyes, image.get_width()*3) # Stretch to three times width
# Blitting takes in the top left position, so we don't need to do any maths here!
screen.blit(eyes, (x,y))
A better approach would be to have two images one for the actual image and one where the eyes are stretched. Draw the second image whenever you need instead of the first image.
import pygame
from pygame.locals import *
screen = pygame.display.set_mode((540, 480))
runner1 = pygame.image.load('./runner1.jpg').convert()
runner1_rect = runner1.get_rect(center=(270, 240))
runner2 = pygame.image.load('./runner2.jpg').convert()
runner2_rect = runner2.get_rect(center=(270, 240))
screen.fill((0, 0, 0))
change = True
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
if event.type == MOUSEBUTTONDOWN:
screen.blit(runner2, runner2_rect)
pygame.display.update()
if event.type == KEYDOWN:
screen.blit(runner1, runner1_rect)
pygame.display.update()
As shown in the above example, we get images(sprites) of different postures and play them as and when we need to get the motion.
To begin and understand the pygame start from PUMMEL THE CHIMP and there are good API to handle sprites and their behavior through pygame.