Created
December 30, 2021 18:30
-
-
Save Xevion/a4f4f76de55dfa1c2e8d066b3348a561 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 shutil | |
from typing import List | |
import imageio | |
import os | |
from PIL import Image, ImageDraw | |
SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__)) | |
IMG_OUT_DIRECTORY = os.path.join(SCRIPT_DIR, 'gif') | |
IMG_TEMP_OUT_DIR = os.path.join(IMG_OUT_DIRECTORY, 'temp') | |
IMG_TEMP_GLOB = os.path.join(IMG_TEMP_OUT_DIR, '*.png') | |
GIF_FINAL_PATH = os.path.join(IMG_OUT_DIRECTORY, 'radial_wipe.gif') | |
os.makedirs(IMG_TEMP_OUT_DIR, exist_ok=True) | |
FRAMES = 25 | |
images: List[str] = [] | |
for i in range(0, FRAMES + 1): | |
w, h = 100, 100 | |
img = Image.new("RGB", (w, h)) | |
imgA = ImageDraw.Draw(img) | |
shape = [(1, 1), (w - 1, h - 1)] | |
progress = i / FRAMES | |
imgA.pieslice(shape, start=90, end=360 * progress + 90, fill="#9ad140") | |
filename = f'{i}.png' | |
temp_output_path = os.path.join(IMG_TEMP_OUT_DIR, filename) | |
with open(temp_output_path, 'wb') as file: | |
img.save(file) | |
images.append(temp_output_path) | |
duration = 0.5 # Seconds | |
calculated_fps = len(images) / duration | |
print(f'Creating GIF at {GIF_FINAL_PATH}') | |
print(f'{duration} seconds, {len(images)} frames, {calculated_fps} fps') | |
with imageio.get_writer(GIF_FINAL_PATH, mode='I', fps=calculated_fps) as writer: | |
for frame in images: | |
writer.append_data(imageio.imread(frame)) | |
shutil.rmtree(IMG_TEMP_OUT_DIR) | |
print('{:.2f} kB written.'.format( | |
os.stat(GIF_FINAL_PATH).st_size / 1024 | |
)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment