简体   繁体   English

PyGame 或 Moviepy - 显示具有透明度的视频

[英]PyGame OR Moviepy - Show a video with transparency

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.所以我使用 PyGame 编写游戏。 I used moviepy to show a video on the surface.我使用moviepy在表面上显示视频。 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.我的初始文件是一个 gif,我转换为 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.我发现 mp4 不支持 alpha/transparency 我尝试使用 gif 通过将video = VideoFileClip('assets/Attacks/Frantz/fireball.mp4')替换为video = VideoFileClip('assets/Attacks/Frantz/fireball.gif')但是白色背景也发生了同样的事情(是的,gif 具有 100% 的透明度)我有点不知道该怎么做。 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.我是否应该尝试其他文件格式,如果是,我该如何删除透明度,但我认为我需要更改代码中的某些内容,以便我可以实际使用 gif 或其他内容。

Heres the file of the gif btw这是gif顺便说一句的文件

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.这些精灵的来源可以是精灵表、动画 GIF 或位图列表。

There are various questions and answers on this topic.关于这个主题有各种各样的问题和答案。 For example:例如:


Since your GIF is not transparent, you have to set the color key for the transparent color with set_colorkey() :由于您的 GIF 不是透明的,因此您必须使用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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM