catm-python-lib
Loading...
Searching...
No Matches
gifgenerator.py
Go to the documentation of this file.
1"""!
2@file gifgenerator.py
3@version 1
4@author Fumitaka ENDO
5@date 2025-06-28T03:55:00+09:00
6@brief gif file generator from pngs
7"""
8from PIL import Image
9import glob
10import time
11import os
12import os
13from datetime import datetime
14import argparse
15
16def generate_gif():
17 """!
18 @brief generate gif from png files
19
20 @details input files and output directory are defined from command line argument
21
22 CLI argument:
23 @arg input input files pathname
24 @arg output output directory
25 """
26 parser = argparse.ArgumentParser()
27 parser.add_argument("input", help="input files path and name with wild card", type=str, default=None)
28 parser.add_argument("output", help="output directory path", type=str, default=None)
29 parser.add_argument("-duration", help="duration time to be genareted gif. default is 200", type=int, default=200)
30
31 args = parser.parse_args()
32 input_path: str = args.input
33 output_path: str = args.output
34 duration_time: int = args.duration
35
36 if os.path.isdir(input_path):
37 inputpath = input_path
38 else:
39 print("input directory does not exist")
40
41 if os.path.isdir(output_path):
42 today_str = datetime.now().strftime("%Y%m%d")
43 dir_name = f"{output_path}/{today_str}"
44 else:
45 print("output directory does not exist")
46
47 current_time = time.time()
48 unix_time_str = str(int(current_time))
49
50 os.makedirs(dir_name, exist_ok=True)
51
52 basepath = f"{dir_name}/*.gif"
53 outputpath = basepath.replace('*', unix_time_str)
54
55 files = sorted(glob.glob(inputpath), key=os.path.getctime)
56
57 images = list(map(lambda file: Image.open(file), files))
58 images[0].save(outputpath, save_all=True, append_images=images[1:], duration=duration_time, loop=0)
59
60 current_directory = os.getcwd()
61
62 full_path = os.path.join(current_directory, outputpath)
63 print(full_path)
64
65