![](/img/trans.png)
[英]How do I overlay a gizeh vector animation onto a moviepy video with transparency?
[英]PyGame OR Moviepy - Show a video with transparency
我的情况很奇怪,但我会尽力解释一切。 所以我使用 PyGame 编写游戏。 我使用moviepy在表面上显示视频。 现在我正在做一个战斗屏幕,我想为攻击设置动画。 我会显示一个快速截图
当点击“火球”时,我想显示一个火球。 视频本身具有透明度,但会用白色填充。
例如,我以前用于显示过场动画的代码如下:
video = VideoFileClip('assets/Attacks/Frantz/fireball.webm')
video.preview()
当它播放视频时,它看起来像这样:
我的初始文件是一个 gif,我转换为 mp4。 我发现 mp4 不支持 alpha/transparency 我尝试使用 gif 通过将video = VideoFileClip('assets/Attacks/Frantz/fireball.mp4')
替换为video = VideoFileClip('assets/Attacks/Frantz/fireball.gif')
但是白色背景也发生了同样的事情(是的,gif 具有 100% 的透明度)我有点不知道该怎么做。 我是否应该尝试其他文件格式,如果是,我该如何删除透明度,但我认为我需要更改代码中的某些内容,以便我可以实际使用 gif 或其他内容。
这是gif顺便说一句的文件
如果很奇怪,我知道我的问题,但它是针对学校项目的,我将不胜感激
电影不是你想要的。 你想要的是一个动画精灵。 动画精灵由许多不同的精灵组成,这些精灵在连续的帧中显示。 这些精灵的来源可以是精灵表、动画 GIF 或位图列表。
关于这个主题有各种各样的问题和答案。 例如:
由于您的 GIF 不是透明的,因此您必须使用set_colorkey()
为透明颜色设置颜色键:
pygameImage.set_colorkey((0, 0, 0))
例子:
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.