Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 2ccfa2e

Browse files
Merge pull request #1112 from SeminKim/animated-image-generator
add new script: Animated image generator
2 parents 4f45737 + 496449b commit 2ccfa2e

File tree

16 files changed

+76
-0
lines changed

16 files changed

+76
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Animated Image Generator
2+
3+
## Aim
4+
5+
This is a simple script to concatenate frame images, generating animation.
6+
7+
## Short description
8+
9+
- With all valid image files in ```--target``` directory, a single animated image will be generated and saved
10+
as ```--output```.
11+
- Other valid options are:
12+
- ```--speed```: How long a single frame will be shown in resulting animation(in milliseconds).
13+
- ```--output_extension```: Instead of designating ```output``` option, one can simply set this to get
14+
results.{extension} file in original target directory.
15+
16+
## Workflow
17+
18+
- This script reads all valid image files using ```glob``` module.
19+
- Valid image files are files with ```IMG_EXTENSIONS```
20+
- Alpha channel will not be used, since it seems gif extension does not support it.
21+
- Then, they are concatenated by ```Pillow.Image``` module.
22+
23+
## Setup instructions
24+
25+
- The only thing to setup is to install Pillow.
26+
27+
```
28+
pip install -r requirements.txt
29+
```
30+
31+
## Output
32+
33+
- Script
34+
```
35+
$ python animated_image_generator.py --target "./images" --output_extension "gif" --speed 25
36+
```
37+
- Result
38+
![output_gif](images/result.gif)
39+
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import argparse
2+
import os
3+
from glob import glob
4+
5+
from PIL import Image
6+
7+
IMG_EXTENSIONS = ['jpg', 'jpeg', 'png'] # Supported Image Extensions
8+
OUTPUT_EXTENSIONS = ['gif', 'webp', 'png'] # Supported Animated Image Extensions
9+
10+
# Argument parsing
11+
parser = argparse.ArgumentParser()
12+
parser.add_argument('--target', help='Target directory which contains frame images.')
13+
parser.add_argument('--output_extension', default='gif',
14+
help='Output file extension. GIF(default), WEBP or APNG(animated png).')
15+
parser.add_argument('--speed', default=25, type=int, help='Milliseconds per frame. Smaller means faster.')
16+
parser.add_argument('--output', default=None, help='Output file name. This overrides `format` argument.')
17+
18+
args = parser.parse_args() # parse all args.
19+
20+
# Find all valid image files.
21+
frames = []
22+
for ext in IMG_EXTENSIONS:
23+
frames.extend(glob(os.path.join(args.target, f'*.{ext}')))
24+
25+
# Open all images
26+
frames = [Image.open(path).convert('RGB') for path in frames]
27+
print(f'{len(frames)} files found')
28+
29+
# Save as an animation
30+
output_path = os.path.join(args.target, f"result.{args.output_extension}")
31+
if args.output is not None:
32+
output_path = args.output
33+
34+
35+
frames[0].save(output_path, save_all=True, append_images=frames[1:], loop=0,
36+
duration=args.speed)
37+
print(f'Save success for {output_path}')
17.3 KB
Loading[フレーム]
19.1 KB
Loading[フレーム]
18.2 KB
Loading[フレーム]
17.2 KB
Loading[フレーム]
17.7 KB
Loading[フレーム]
18.3 KB
Loading[フレーム]
18.4 KB
Loading[フレーム]
18.4 KB
Loading[フレーム]

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /