Created
October 9, 2024 05:32
-
-
Save faywong/52dd85ba8787d3cf7b6957eb59718333 to your computer and use it in GitHub Desktop.
gif compress
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
import os | |
import glob | |
# 使用 glob 模块搜索所有 .gif 文件 | |
gif_files = glob.glob(os.path.join('in', '**', '*.gif'), recursive=True) | |
def ensure_dir(file_path): | |
# 获取文件路径的目录部分 | |
directory = os.path.dirname(file_path) | |
# 如果目录为空(即文件在当前目录),使用当前目录 | |
if not directory: | |
directory = '.' | |
# 如果目录不存在,创建它 | |
if not os.path.exists(directory): | |
os.makedirs(directory) | |
# 打印找到的 .gif 文件 | |
for file in gif_files: | |
path_parts = file.split('/') | |
path_parts[0] = 'out' | |
out_gif = os.path.join(*path_parts) | |
ensure_dir(out_gif) | |
os.system('gifsicle -O2 --lossy=80 %s -o %s' % (file, out_gif)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment