-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocess.py
67 lines (47 loc) · 1.55 KB
/
process.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
#!/usr/bin/python3
from PIL import Image
import os
import sys
folder = sys.argv[1]
args = sys.argv[2:]
if len(args) == 1 and '-' in args[0]:
args = range(int(args[0].split('-')[0]), int(args[0].split('-')[1]) + 1)
else:
args = [int(arg) for arg in args]
width = 5161
height = 7309
uncrop_width = 6071
uncrop_height = 8599
def thresh(img, threshold = 5):
return img.point(lambda p: 255 * (p > threshold))
def remove_alpha(img):
background = Image.new('RGBA', img.size, (255,255,255))
img = Image.alpha_composite(background, img)
def kra2png(filename):
imagename = filename.split('.')[0][:2] + filename.split('.')[0][2:].zfill(3) + ".png"
print(imagename)
os.system(f'krita {filename} --export --export-filename {imagename}')
return imagename
def process(imagename):
img = Image.open(f"{imagename}").convert('L')
img = thresh(img)
left = (uncrop_width - width)//2
right = width + left
top = (uncrop_height - height)//2
bottom = height + top
if img.width > 2*width: # 2 page spread case
img = img.crop((left, top, right + width, bottom))
else:
img = img.crop((left, top, right, bottom))
img.save(f"out/{imagename}")
if not os.path.exists(folder + "/out"):
os.makedirs(folder + "/out")
os.chdir(folder)
def getNum(filename):
return int(filename.split('pg')[1].split('.')[0])
for f in os.listdir(folder):
if "pg" not in f or '.kra' not in f or '~' in f:
continue
if args and getNum(f) not in args:
continue
process(kra2png(f))