-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautosplit.py
executable file
·57 lines (44 loc) · 1.85 KB
/
autosplit.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
#!/usr/bin/python2
# -*- coding: utf-8 -*-
from os import path
from PIL import Image
from utils import makeSrcDst, imageFilter
def autoSplit(im, threshold=1.0):
width, height = im.size
if 1.0 * width / height > threshold:
split_width = width / 2
im1 = im.crop((0, 0, split_width, height))
im2 = im.crop((split_width, 0, width, height))
return [im1, im2]
return [im]
def main(source, destination, **options):
outfmt = options['outfmt']
threshold = options['threshold']
for source, destination in makeSrcDst(source, destination,
src_filter=imageFilter):
im = Image.open(source)
im.load()
ims = autoSplit(im, threshold=threshold)
parts = len(ims)
for part, im in enumerate(ims, 1):
directory, basename = path.split(destination)
root, ext = path.splitext(basename)
new_root = outfmt.format(root=root, part=part,
part_rev=parts+1-part)
outfile = path.join(directory, new_root + ext)
im.save(outfile)
if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser(
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
description='Auto split double page comic pictures')
parser.add_argument('source', type=str, # nargs='+',
help='input image file/directory')
parser.add_argument('destination', type=str,
help='output image file/directory')
parser.add_argument('--threshold', type=float, default=1.0,
help='if width/height > threshold, split')
parser.add_argument('--outfmt', type=str, default='{root}_{part}',
help='output format')
args = parser.parse_args()
main(**vars(args))