Skip to main content

One post tagged with "python"

View All Tags

· One min read
Brandon Poole

If you look at the #lego blog posts, you'll see GIFs at the bottom of the different stages of the build. While I'm sure there's tools on the internet for this, that's no fun compared to writing your own. These were achieved with the following Python script:

import imageio
import imageio.v3 as iio
from skimage.transform import resize
import os

# Set the directory containing the JPG files. Yes, I'm on Windows.
input_dir = Path(r"C:\Users\path\to\LEGO AT-AT")

# Set the output GIF file path
output_path = Path(r"C:\Users\path\to\build.gif")

# Get a list of all JPG files in the input directory.
jpg_files = [f for f in os.listdir(input_dir) if f.endswith('.jpg')]

# Create an empty list to hold the images
images = []

# Loop through the JPG files and add them to the images list
for jpg_file in jpg_files:
file_path = os.path.join(input_dir, jpg_file)
image = iio.imread(file_path)
# Reduce size from Pxiel phone's 4k to manageable size for personal website.
image = resize(image, (image.shape[0]*0.25, image.shape[1]*0.25), anti_aliasing=True)
image = (image*255).astype(np.uint8)
images.append(image)

# Save the images as a GIF
imageio.mimsave(output_path, images, duration=.75, loop=0)