-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwatermark_remover.py
29 lines (23 loc) · 1.24 KB
/
watermark_remover.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
import fitz
class WatermarkRemover:
def remove_watermarks(self, pdf_path, images_to_remove_info, output_pdf_path="output_without_watermarks.pdf"):
try:
pdf_document = fitz.open(pdf_path)
removed_count = 0
for image_info in reversed(images_to_remove_info):
page_num = image_info['page']
xref = image_info['xref']
page_to_modify = pdf_document[page_num]
try:
page_to_modify.delete_image(xref)
removed_count += 1
print(f"Removed watermark image xref:{xref} (name '{image_info['image_name']}') from page {page_num + 1}.")
except Exception as remove_error:
print(f"Error removing watermark image xref:{xref} (name '{image_info['image_name']}') on page {page_num + 1}: {remove_error}")
pdf_document.save(output_pdf_path)
pdf_document.close()
print(f"\nNew PDF without watermarks saved as: {output_pdf_path}")
print(f"Total watermarks removed: {removed_count}")
return output_pdf_path, None
except Exception as e:
return None, f"Error removing watermarks and saving PDF: {str(e)}"