Last active
October 24, 2025 04:44
-
-
Save kaixinol/45cd9cb38aa1b44ccf5ffa9d67758c11 to your computer and use it in GitHub Desktop.
批量分割QQ表情包为每120一组
This file contains hidden or 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
| import re | |
| import shutil | |
| from pathlib import Path | |
| def organize_stickers(): | |
| """ | |
| 整理线条小狗贴纸文件夹,按每120个文件为一组生成新文件夹 | |
| """ | |
| current_dir = Path('.') | |
| # 查找所有匹配的文件夹 | |
| pattern = re.compile(r'^\d+-线条小?狗(\d+)$') | |
| folders = [] | |
| for item in current_dir.iterdir(): | |
| if item.is_dir(): | |
| match = pattern.match(item.name) | |
| if match: | |
| series_num = int(match.group(1)) | |
| folders.append((series_num, item)) | |
| # 按系列序号排序 | |
| folders.sort(key=lambda x: x[0]) | |
| if not folders: | |
| print('未找到匹配的文件夹') | |
| return | |
| print(f'找到 {len(folders)} 个系列文件夹:') | |
| for num, folder in folders: | |
| print(f' 系列 {num}: {folder.name}') | |
| # 检测并报告缺失的系列 | |
| if folders: | |
| series_numbers = [num for num, _ in folders] | |
| min_series = min(series_numbers) | |
| max_series = max(series_numbers) | |
| expected_series = set(range(min_series, max_series + 1)) | |
| found_series = set(series_numbers) | |
| missing_series = sorted(expected_series - found_series) | |
| if missing_series: | |
| print(f'\n⚠️ 缺失的系列序号: {", ".join(map(str, missing_series))}') | |
| else: | |
| print(f'\n✓ 系列序号连续完整({min_series}-{max_series})') | |
| # 收集所有gif文件 | |
| all_gifs = [] | |
| for series_num, folder in folders: | |
| images_dir = folder / 'images' | |
| if images_dir.exists(): | |
| gif_files = sorted(images_dir.glob('*.gif')) | |
| for gif in gif_files: | |
| all_gifs.append((series_num, gif)) | |
| print(f'系列 {series_num}: 找到 {len(gif_files)} 个文件') | |
| print(f'\n总共找到 {len(all_gifs)} 个GIF文件') | |
| # 按每120个文件分组 | |
| batch_size = 120 | |
| batch_num = 1 | |
| for i in range(0, len(all_gifs), batch_size): | |
| batch = all_gifs[i : i + batch_size] | |
| # 获取起始和结束的系列序号 | |
| start_series = batch[0][0] | |
| end_series = batch[-1][0] | |
| # 创建新文件夹 | |
| if start_series == end_series: | |
| folder_name = f'线条小狗{start_series}(系列序号{start_series})' | |
| else: | |
| folder_name = f'线条小狗{start_series}-{end_series}(系列序号{start_series}到{end_series})' | |
| output_dir = current_dir / folder_name | |
| output_dir.mkdir(exist_ok=True) | |
| # 复制文件 | |
| print(f'\n创建文件夹: {folder_name}') | |
| print(f' 包含 {len(batch)} 个文件') | |
| for idx, (series_num, gif_path) in enumerate(batch, 1): | |
| # 保持原文件名或添加序号前缀 | |
| new_name = f'{idx:03d}_{gif_path.name}' | |
| dest_path = output_dir / new_name | |
| shutil.copy2(gif_path, dest_path) | |
| print(' ✓ 已复制完成') | |
| batch_num += 1 | |
| print(f'\n整理完成!共生成 {batch_num - 1} 个文件夹') | |
| try: | |
| organize_stickers() | |
| except Exception as e: | |
| print(f'错误: {e}') | |
| import traceback | |
| traceback.print_exc() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment