Skip to content

Instantly share code, notes, and snippets.

@curegit
Last active August 12, 2023 23:34
Show Gist options
  • Save curegit/8b292cf40fbf05bd346f51a9ce2d78e2 to your computer and use it in GitHub Desktop.
Save curegit/8b292cf40fbf05bd346f51a9ce2d78e2 to your computer and use it in GitHub Desktop.
複数のアニメーション画像をタイル状に並べて一つの画像にするスクリプト
#!/usr/bin/env python3
x = 3
y = 2
background = "white"
gap = 10
duration = 100
anims = [
"APNGs/a1.png",
"APNGs/a2.png",
"APNGs/a3.png",
"APNGs/a4.png",
"APNGs/a5.png",
"APNGs/a6.png",
]
out = "out.png"
import itertools
from PIL import Image, ImageSequence
def tile(x, y, imgs, gap, background):
imgs = iter(imgs)
img = next(imgs)
w, h = img.size
canvas = Image.new("RGB", (w * x + gap * (x - 1), h * y + gap * (y - 1)), background)
for j, i in itertools.product(range(y), range(x)):
canvas.paste(img, (i * (w + gap), j * (h + gap)))
img = next(imgs, None)
if img is None:
break
img = img.convert("RGB")
return canvas
images = []
for frames in zip(*[ImageSequence.Iterator(Image.open(anim)) for anim in anims]):
frame = tile(x, y, frames, gap, background)
images.append(frame)
images[0].save(out, save_all=True, duration=duration, append_images=images[1:], loop=0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment