-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
193 lines (152 loc) · 7.08 KB
/
main.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# Develped by ssYanhuo.
import math
import fitz
from tqdm import tqdm
import numpy as np
import os
def is_number(s):
try:
float(s)
return True
except ValueError:
pass
try:
import unicodedata
unicodedata.numeric(s)
return True
except (TypeError, ValueError):
pass
return False
def is_colour_page(page, threshold):
pixel_map = page.get_pixmap()
if pixel_map.n <= 1:
return False
pixel_matrix = np.frombuffer(pixel_map.samples, dtype=np.uint8).reshape(pixel_map.h, pixel_map.w, pixel_map.n)
red_channel = pixel_matrix[:, :, 0]
green_channel = pixel_matrix[:, :, 1]
blue_channel = pixel_matrix[:, :, 2]
return (
(np.abs(red_channel - green_channel) > threshold).any() or
(np.abs(red_channel - blue_channel) > threshold).any() or
(np.abs(green_channel - blue_channel) > threshold).any()
)
def generate_action_list(input_list):
result = []
current = input_list[0]
c = 1
for state_index in range(1, len(input_list)):
if input_list[state_index] == current:
c += 1
else:
result.append({'colour': current, 'count': c})
current = input_list[state_index]
c = 1
result.append({'colour': current, 'count': c})
return result
if __name__ == '__main__':
print('▄▄▄▄▄▄▄ ▄▄▄▄▄▄▄ ▄▄ ▄▄ ▄▄▄▄▄▄ ▄▄ ▄ ▄▄ ▄▄ ▄▄ ▄▄ ▄▄▄▄▄▄▄ ')
print('█ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ ')
print('█ ▄▄▄▄▄█ ▄▄▄▄▄█ █▄█ █ ▄ █ █▄█ █ █▄█ █ █ █ █ ▄ █ ')
print('█ █▄▄▄▄▄█ █▄▄▄▄▄█ █ █▄█ █ █ █ █▄█ █ █ █ █ ')
print('█▄▄▄▄▄ █▄▄▄▄▄ █▄ ▄█ █ ▄ █ ▄ █ █ █▄█ █ ')
print('▄▄▄▄▄█ █▄▄▄▄▄█ █ █ █ █ ▄ █ █ █ █ █ █ █ █ █ ')
print('█▄▄▄▄▄▄▄█▄▄▄▄▄▄▄█ █▄▄▄█ █▄█ █▄▄█▄█ █▄▄█▄▄█ █▄▄█▄▄▄▄▄▄▄█▄▄▄▄▄▄▄█ ')
print()
print('Developed by ssYanhuo.')
print('Star on GitHub: ' + 'https://github.com/ssYanhuo/pdf-colour-split')
print()
input_file_path = input('要拆分的文件路径(可直接拖入):')
output_file_path = input('输出文件夹路径(可直接拖入):')
input_file_path = input_file_path.strip('"').strip('\'')
output_file_path = output_file_path.strip('"').strip('\'')
double_page = input('单面打印 (s) OR 双面打印 (d, 默认):').lower() != 's'
detect_threshold = input('检测阈值 (10, 默认):') or '10'
if not is_number(detect_threshold):
detect_threshold = 10
else:
detect_threshold = int(detect_threshold)
print()
print('开始拆分')
print('拆分文件:' + input_file_path)
print('输出文件夹:' + output_file_path)
print('打印方式:' + '双面打印' if double_page else '单面打印')
print('检测阈值:' + str(detect_threshold))
input_pdf = fitz.open(input_file_path)
output_grayscale_pdf = fitz.open()
output_colour_pdf = fitz.open()
count = {'total': input_pdf.page_count,
'print': math.ceil(input_pdf.page_count / 2 if double_page else input_pdf.page_count),
'grayscale': 0,
'colour': 0}
print('总页数:' + str(count['total']))
print('打印张数:' + str(count['print']))
colour_state_list = []
colour_print_state_list = []
print()
for i in tqdm(range(0, count['total'], 2 if double_page else 1)):
unit_pages = [input_pdf[i]]
if double_page and i + 1 < input_pdf.page_count:
unit_pages.append(input_pdf[i + 1])
has_colour_page = False
for page in unit_pages:
if is_colour_page(page, detect_threshold):
colour_state_list.append(True)
has_colour_page = True
else:
colour_state_list.append(False)
for page in unit_pages:
if has_colour_page:
output_colour_pdf.insert_pdf(input_pdf, from_page=page.number, to_page=page.number)
else:
output_grayscale_pdf.insert_pdf(input_pdf, from_page=page.number, to_page=page.number)
if has_colour_page:
colour_print_state_list.append(True)
count['colour'] += 1
else:
colour_print_state_list.append(False)
count['grayscale'] += 1
print()
if output_grayscale_pdf.page_count > 0:
output_grayscale_path = '{}{}{}.pdf'.format(
os.path.join(output_file_path, os.path.splitext(os.path.basename(input_file_path))[0]),
'_grayscale',
'_double' if double_page else '_single')
print('写入文件(黑白):' + output_grayscale_path)
output_grayscale_pdf.save(output_grayscale_path)
if output_colour_pdf.page_count > 0:
output_colour_path = '{}{}{}.pdf'.format(
os.path.join(output_file_path, os.path.splitext(os.path.basename(input_file_path))[0]),
'_colour',
'_double' if double_page else '_single')
print('写入文件(彩色):' + output_colour_path)
output_colour_pdf.save(output_colour_path)
print()
print('拆分完成!')
print('\033[0;33m务必检查拆分后的文件,如果拆分错误,请修改检测阈值\033[0m')
grayscale_page_number_list = []
colour_page_number_list = []
for index, state in enumerate(colour_state_list):
if state:
colour_page_number_list.append(str(index + 1))
else:
grayscale_page_number_list.append(str(index + 1))
print('黑白页(共打印 {} 张)页码:\n{}'.format(count['grayscale'], ', '.join(grayscale_page_number_list)))
print('彩色页(共打印 {} 张)页码:\n{}'.format(count['colour'], ', '.join(colour_page_number_list)))
action_list = generate_action_list(colour_print_state_list)
print()
print('按照以下方法把打印好的文稿重新组合成原文稿(可复制保存此段备用):')
print('将打印好的黑白与彩色文稿分别按页码从小到大,自上而下排序(直接拿起打印好的文稿,正面朝上即可)')
print('组合时拿取指定张数的黑白或彩色文稿,一起翻面扣放在整理好的文稿上面')
print()
for index, action in enumerate(action_list):
print('{}{}. 取 {} 张 {} 文稿,一起翻面扣放在整理好的文稿上面{}'.format(
'\033[0;32m' if action['colour'] else '\033[0;34m',
index + 1,
action['count'],
'彩色' if action['colour'] else '黑白',
'\033[0m'
))
print()
print('整理完成!')
print()
input('按任意键退出……')