Last active
May 11, 2017 08:15
-
-
Save Restioson/6309a3f37e4fb2adb5808e1883f8b56a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import arcade | |
import time | |
import glob | |
from PIL import Image | |
from PIL import ImageSequence | |
import os | |
import shutil | |
def split_textures(path): | |
# Clear frames.tmp | |
if os.path.exists("frames.tmp"): | |
shutil.rmtree("frames.tmp") if os.path.isdir("frames.tmp") else os.remove("frames.tmp") | |
os.mkdir("frames.tmp") | |
gif = Image.open(path) | |
len_frames = 0 | |
for frame in ImageSequence.Iterator(gif): | |
len_frames += 1 | |
frame.save(os.path.join("frames.tmp", "{0}_{1}.gif".format(len_frames, frame.info["duration"])), "GIF") | |
def load_textures(path): | |
split_textures(path) | |
return [(arcade.load_texture("{0}".format(filename)), int(filename.split(".gif")[0].split("_")[1])) for filename in glob.glob(os.path.join("frames.tmp", "*.gif"))] | |
def render(frames): | |
for texture, delay in frames: | |
before_render = time.time() | |
arcade.start_render() | |
arcade.draw_xywh_rectangle_filled(0, 0, texture.width, texture.height, arcade.color.WHITE) | |
arcade.draw_texture_rectangle(texture.width / 2, texture.height / 2, texture.width, texture.height, texture) | |
arcade.finish_render() | |
time.sleep(delay / 1000 - (before_render - time.time())) | |
def run(): | |
gif_path = input("Path to gif > ") | |
textures = load_textures(gif_path) | |
arcade.open_window(gif_path.split(os.path.sep)[-1], textures[0][0].width, textures[0][0].height) | |
arcade.set_background_color(arcade.color.WHITE) | |
while True: | |
render(textures) | |
run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment