Skip to content

Instantly share code, notes, and snippets.

@boppreh
Last active May 5, 2023 17:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save boppreh/3607a043aa1fb9f539df26f67a89b64b to your computer and use it in GitHub Desktop.
Save boppreh/3607a043aa1fb9f539df26f67a89b64b to your computer and use it in GitHub Desktop.
Creates two GIFs, where the second one swaps the Y and time axis.
import sys
from PIL import Image, ImageDraw
if len(sys.argv) > 1:
gif_path, = sys.argv[1:]
im = Image.open(gif_path)
width, before_height = im.size
images_before = [im.copy()]
while im.tell() < im.n_frames-1:
im.seek(im.tell()+1)
images_before.append(im.copy())
else:
width = before_height = 200
images_before = []
# Default pre-rotation animation adapted from https://note.nkmk.me/en/python-pillow-gif/
center = width // 2
color_1 = (0, 0, 0)
color_2 = (255, 255, 255)
max_radius = int(center * 1.5)
step = 2
for i in range(0, max_radius, step):
im = Image.new('RGB', (width, before_height), color_1)
draw = ImageDraw.Draw(im)
draw.ellipse((center - i, center - i, center + i, center + i), fill=color_2)
images_before.append(im)
for i in range(0, max_radius, step):
im = Image.new('RGB', (width, before_height), color_2)
draw = ImageDraw.Draw(im)
draw.ellipse((center - i, center - i, center + i, center + i), fill=color_1)
images_before.append(im)
images_before[0].save('time_rotation_before.gif',
save_all=True, append_images=images_before[1:], optimize=False, duration=40, loop=0)
# Create a new stack of images swapping the Y and time axis.
after_height = len(images_before)
after_n_frames = before_height
pixels_after = [[(0, 0, 0)]*width*after_height for _ in range(after_n_frames)]
for after_y, image_before in enumerate(images_before):
pixels = list(image_before.getdata())
assert len(pixels) == width * before_height
for before_y in range(before_height):
row = pixels[width*before_y:width*(before_y+1)]
pixels_after[before_y][width*after_y:width*(after_y+1)] = row
images_after = []
for pixels in pixels_after:
img = Image.new('RGB', (width, after_height))
img.putdata(pixels)
images_after.append(img)
images_after[0].save('time_rotation_after.gif',
save_all=True, append_images=images_after[1:], optimize=False, duration=40, loop=0)
@boppreh
Copy link
Author

boppreh commented May 5, 2023

Before:
time_rotation_before

After:
time_rotation_after


Before:
1130283_before

After:
1130283_after

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment