-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCSMarkRemover.py
66 lines (53 loc) · 2.11 KB
/
CSMarkRemover.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
# Simple Python script for batch-removing the watermark from the app CamScanner on a folder of PDFs -- created by Allan Edh ([email protected])
# Requirements: 'pip install PyMuPDF'
import fitz
import os
import sys
# Crop function
def crop_pdf(input_folder, crop_left, crop_right, crop_top, crop_bottom):
output_folder = os.path.join(os.path.dirname(input_folder), os.path.basename(input_folder) + '_NoMark')
# Create output folder
if not os.path.exists(output_folder):
os.makedirs(output_folder)
# Iterate through files
for filename in os.listdir(input_folder):
if filename.endswith('.pdf'):
input_path = os.path.join(input_folder, filename)
output_path = os.path.join(output_folder, filename)
doc = fitz.open(input_path)
for page in doc:
rect = page.rect
# Is the page horisontal
is_horizontal = rect.width > rect.height
if is_horizontal:
# If horisontal crop only bottom
new_rect = fitz.Rect(
rect.x0,
rect.y0,
rect.x1,
rect.y1 - crop_bottom
)
else:
# If vertical crop sides too
new_rect = fitz.Rect(
rect.x0 + crop_left,
rect.y0 + crop_top,
rect.x1 - crop_right,
rect.y1 - crop_bottom
)
page.set_cropbox(new_rect)
# Save output
doc.save(output_path)
doc.close()
# Main
if __name__ == "__main__":
if len(sys.argv) != 2:
print("Usage: python CSMarkRemover.py <input-folder>")
sys.exit(1)
input_folder = sys.argv[1]
# EDIT CROP VALUES IF NEEDED:
crop_left = 25
crop_right = 25
crop_top = 0
crop_bottom = 50
crop_pdf(input_folder, crop_left, crop_right, crop_top, crop_bottom)