-
Notifications
You must be signed in to change notification settings - Fork 0
/
joycrop.py
42 lines (34 loc) · 1.34 KB
/
joycrop.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
import argparse
from pathlib import Path
from PIL import Image
def crop_image(file):
img = Image.open(file)
width, height = img.size
return img.crop((0, 0, width, height - 14))
def process(iterator, *, level=0, backup=False):
for item in iterator:
if not item.exists():
print(f"[warn]: file {item} not exist")
continue
if item.is_dir():
if level > 0:
print("[warn]: subfolder `{item}` bypass disabled")
return
process(item.iterdir(), level=level + 1, backup=backup)
elif item.is_file():
output = str(item)
if backup:
output = "crop_" + output
print(f"[process]: {item} -> {output}")
else:
print(f"[process]: {item}")
img = crop_image(item)
img.save(output)
else:
print(f"[warn]: type of {item} is not supported")
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="joyreactor banner crop app")
parser.add_argument("-i", dest="input", metavar="input", type=Path, nargs="+", required=True, help="input files")
parser.add_argument("-b", dest="backup", action="store_true", default=False, help="save original file")
args = parser.parse_args()
process(args.input, backup=args.backup)