Skip to content

Commit

Permalink
Added a script to convert a folder of images to a small .gif
Browse files Browse the repository at this point in the history
  • Loading branch information
sha016 committed Oct 26, 2021
1 parent f92a6a7 commit ef15b96
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 0 deletions.
22 changes: 22 additions & 0 deletions Python/Images_to_GIF/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# images_to_gif.py
A simple Python script that takes a directory of images, resizes them, and combines them into a .gif file.

## How to use
python images_to_gif.py /fullpath/to/image/folder

## Also this will work
python images_to_gif.py
> Enter fullpath to directory: /fullpath/to/image/folder
## requirements
imageio
Pillow

Pillow is a PIL fork.

## Output
Files will be created under gifs/yourfolder and resized/
![Image 1](https://i.ibb.co/grD9gnn/images-to-gif.png)

## Author
[sha016](https://github.com/sha016)
101 changes: 101 additions & 0 deletions Python/Images_to_GIF/images_to_gif.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
#!/usr/bin/env python
""" Provides a script that takes a (full) path to a directory of image files and converts it to a
.gif file in the current working directory under a folder called 'gifs'. There will be a subfolder
based on the parent folder of the images. """
import os
import sys
import math

from PIL import Image
import imageio

try:
user_input = raw_input # python2.x compat
except NameError:
user_input = input

EXTENSIONS = [
".jpg",
".jpeg",
".png",
]
GIF_FRAMES = 120


def in_extensions(file):
""" Check if a file has a valid extension. """
return any(file.lower().endswith(ext) for ext in EXTENSIONS)


def main():
"""Turns a directory of images into a smaller GIF."""
if len(sys.argv) < 2:
print("Usage: images_to_gif.py /fullpath/to/images/directory")
inp = user_input("Enter fullpath to directory: ")
if not inp:
print("No directory provided, exiting.")
sys.exit()
else:
sys.argv.append(inp)

base_dir = sys.argv[1]
working_dir = os.getcwd()
output_dir = os.path.join(working_dir, "gifs")

if not os.path.exists(output_dir):
os.makedirs(output_dir)
print(f"Created output directory: {output_dir}")

folder_name = os.path.dirname(base_dir).split("/")[-1]
gif_dir = os.path.join(output_dir, folder_name)
if not os.path.exists(gif_dir):
os.makedirs(gif_dir)

save_dir = os.path.join(working_dir, "resized", folder_name)

if not os.path.isdir(save_dir):
os.makedirs(save_dir)
print(f"Created resized img directory {save_dir}")

for file in sorted(os.listdir(base_dir)):
path = os.path.join(base_dir, file)
if os.path.isfile(os.path.join(save_dir, file)):
continue
try:
img = Image.open(path)
except IOError as err:
print(f"Error opening {file}", err)
continue
try:
img = img.resize((256, 192), Image.ANTIALIAS)
img.save(save_dir + "/" + file)
except OSError as err2:
print("Error resizing ", err2)
continue

resized = []
count = 0

# imageio takes files as input
for file in sorted(os.listdir(save_dir)):
if not in_extensions(file):
continue

path = os.path.join(save_dir, file)
tiny_img = imageio.imread(path)
resized.append(tiny_img)
suffix = folder_name + str(1 + (math.floor(count / GIF_FRAMES))) + ".gif"
save_path = os.path.join(gif_dir, suffix)
count += 1

# create .gif files with a number of image files equal to GIF_FRAMES
if not len(resized) % GIF_FRAMES:
imageio.mimsave(save_path, resized)
resized = []

imageio.mimsave(save_path, resized)
print(f"GIF saved to {gif_dir}")


if __name__ == "__main__":
main()
2 changes: 2 additions & 0 deletions Python/Images_to_GIF/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Pillow
imageio

0 comments on commit ef15b96

Please sign in to comment.