Created
December 31, 2021 03:16
-
-
Save Xevion/14902f6f291458acf0af135d25c3cfb6 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 = 50 | |
images: List[str] = [] | |
for i in range(0, FRAMES): | |
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 | |
if calculated_fps > 100.0: | |
print('Warning: GIF images are not designed to exceed 100 FPS.') | |
print('\tIncrease duration to a minimum of {dur:.2f}s or decrease frame count to a maximum of {count:.2f} frames to fit.'.format( | |
dur=FRAMES / 100, count=100 * 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