-
Notifications
You must be signed in to change notification settings - Fork 7
/
regroupImages.py
98 lines (88 loc) · 3.43 KB
/
regroupImages.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
import argparse
import os
from os import walk
from os.path import join
from shutil import copyfile
from zipfile import ZipFile
images_type = ["jpg", "jpeg", "png", "webp"]
HARD_LIMIT = 170
USE_CBZ = True
DEFAULT_NAME = "Vol "
def regroup_all_images(folder):
out_folder = folder
count_images = 0
count_folder = 1
volume = DEFAULT_NAME + "01"
os.makedirs(out_folder, exist_ok=True)
os.makedirs(join(out_folder, volume))
current_dir = ""
for root, dirs, files in walk(folder):
for i in files:
ext_file = i.split(".")[-1].lower()
if ext_file in images_type:
print("FOUND:", root, i)
if count_images == HARD_LIMIT:
current_dir = root
if count_images > HARD_LIMIT and current_dir != root:
current_dir = root
count_folder += 1
count_images = 0
volume = DEFAULT_NAME + str(count_folder).zfill(2)
os.makedirs(join(out_folder, volume), exist_ok=True)
input_path = join(root, i)
output_path = join(
out_folder, volume, str(count_images).zfill(3) + "." + ext_file
)
copyfile(input_path, output_path)
count_images += 1
def regroup_all_images_cbz(folder, delete=False):
out_folder = folder
count_images = 0
count_folder = 1
volume = DEFAULT_NAME + "01.cbz"
os.makedirs(out_folder, exist_ok=True)
current_dir = ""
cbz_file = None
for root, dirs, files in walk(folder):
dirs.sort()
files.sort()
if root == folder:
continue
for i in files:
ext_file = i.split(".")[-1].lower()
if ext_file in images_type:
print("FOUND:", root, i)
if count_images == HARD_LIMIT:
current_dir = root
if count_images > HARD_LIMIT and current_dir != root:
current_dir = root
count_folder += 1
count_images = 0
cbz_file.close()
volume = DEFAULT_NAME + str(count_folder).zfill(2) + ".cbz"
cbz_file = ZipFile(join(out_folder, volume), "w")
input_path = join(root, i)
output_path = str(count_images).zfill(3) + "." + ext_file
if not cbz_file:
# Lazy initialisation for empty folders
cbz_file = ZipFile(join(out_folder, volume), "w")
cbz_file.write(input_path, output_path)
if delete:
os.remove(input_path)
count_images += 1
if delete:
print("DELETE", root)
os.removedirs(root)
if cbz_file:
cbz_file.close()
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Regroup all images inside a cbz according to a limit."
)
parser.add_argument("path", help="folder to rename", nargs="+")
parser.add_argument("--delete", help="move and not copy", action="store_true")
parser.add_argument("--limit", help="number of images per tome", default=HARD_LIMIT)
args = parser.parse_args()
HARD_LIMIT = args.limit
for path in args.path:
regroup_all_images_cbz(path, args.delete)