-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgif_maker.py
121 lines (101 loc) · 4.4 KB
/
gif_maker.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import argparse
import glob
import os
from datetime import datetime
from PIL import Image
from rich import print
from rich.progress import track
from rich.console import Console
from config import check_config
parser = argparse.ArgumentParser(
description="Resizes and creates gif out of png files in specified directory",
epilog="And then there was gif...",
prog='Gif Maker'
)
parser.add_argument('-c', '--config', help="pass in -c or --config to use a config.", action='store_true', required=False)
parser.add_argument('-d', '--directory', help="directory where files are located.", required=False)
parser.add_argument('-p', '--prefix', help="prefix to filter files.", required=False)
parser.add_argument('-s', '--scale', help="width value to scale final output, defaults to 400", required=False)
parser.add_argument('-e', '--extension', help="file extenstion to filter by.", required=False)
parser.add_argument('--clean', help='clean up resized images, default', action='store_true')
parser.add_argument('-L', '--leave', help='leave original and processed files, default app removes all files except gif output.', action='store_false')
parser.add_argument('-o', '--output', help="directory to output gif.", required=False)
parser.add_argument('--version', action='version', version='%(prog)s 0.2')
args = parser.parse_args()
LEAVE = args.leave
CLEAN = args.clean
def scale_image(input_image_path, output_image_path, width):
"""
resize images before making gif.
"""
original_image = Image.open(input_image_path)
w, h = original_image.size
max_size = (width, h)
original_image.thumbnail(max_size, Image.Resampling.LANCZOS)
original_image.save(output_image_path)
def remove_quotes(image_path):
return image_path.replace('"', '').replace("'", "")
def resize_images(image_path, glob_regex, width) -> int:
file_count = 0
print(f'Resizing images in directory: {image_path}')
search = os.path.join(image_path, glob_regex)
for image in track(glob.glob(search)):
out = image + '.resized.png'
export_folder = os.path.join(image_path, out)
scale_image(image, export_folder, width)
file_count += 1
print(f'resized {file_count} images')
return file_count
def clean_up(search, image_path):
if LEAVE:
print('[bold yellow]removing resized files[/bold yellow]')
for image in track(glob.glob(search), description=f'cleaning up files {search}'):
os.remove(image)
if CLEAN:
# TODO clean all files using image path.
pass
def make_gif(image_path, glob_regex, output_path, gif_name=None):
gif_name = gif_name or f'{datetime.now().strftime("%Y%m%d%H%M")}_gif_maker.gif'
search = os.path.join(image_path, glob_regex)
outfile = os.path.join(output_path, gif_name) if output_path else os.path.join(image_path, gif_name)
console = Console()
with console.status("[bold green]generating the gif... ") as status:
frames = [Image.open(image) for image in glob.glob(search)]
frame_one = frames[0]
frame_one.save(
outfile, format="GIF",
append_images=frames,
save_all=True,
duration=100,
loop=0
)
print(f'gif created: {gif_name}')
clean_up(search, image_path)
def generate_filter(args):
if (args.prefix and args.extension):
return f'{args.prefix}*.{args.extension}'
if (args.prefix or args.extension):
return f'{args.prefix}*' if args.prefix else f'*.{args.extension}'
return 'vlcsnap*.png' # TODO default will be set in config
def main():
if args.config:
check_config()
# parser.print_help()
return
if not args.directory:
print('[bold red]Please enter a path to image files with -d flag.[/bold red]')
return
fp_in = args.directory
fp_in = remove_quotes(fp_in)
output_path = args.output if args.output else None
arg_width = int(args.scale) if args.scale else 400
file_filter_to_resize = generate_filter(args)
print(f'file filter: [yellow]{file_filter_to_resize}[/yellow]')
count = resize_images(fp_in, file_filter_to_resize, arg_width)
if count >= 1:
resized_files = f'{file_filter_to_resize[0:-4]}.resized.png'
make_gif(fp_in, resized_files, output_path=output_path)
else:
print('[yellow]No files found. Check file path/filters and try again bob[/yellow]')
if __name__ == "__main__":
main()