arunavabasucom commited on
Commit
4b7c9ce
·
unverified ·
0 Parent(s):

feat: initial commit

Browse files
Files changed (6) hide show
  1. .gitignore +5 -0
  2. README.md +1 -0
  3. main.py +125 -0
  4. main2.py +36 -0
  5. requirements.py +0 -0
  6. run.sh +0 -0
.gitignore ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ output
2
+ images
3
+ *.jpeg
4
+ *.pdf
5
+ *.png
README.md ADDED
@@ -0,0 +1 @@
 
 
1
+ # color_inverter_py
main.py ADDED
@@ -0,0 +1,125 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import cv2
2
+ import re
3
+ import os
4
+ import glob
5
+ import sys
6
+ from fpdf import FPDF
7
+ from pdf2image import convert_from_path
8
+ from PIL import Image as PILImage
9
+
10
+ class Converter:
11
+ def __init__(self):
12
+ self.a4_w_mm = 210
13
+ self.a4_h_mm = 297
14
+ self.dpi = 200.0
15
+
16
+ def invert_image(self, i_input, i_output):
17
+ """Inverts a given image."""
18
+ image = cv2.imread(i_input)
19
+ print("Inverting image: {}".format(i_input))
20
+ if image is None:
21
+ print("Error reading image: {}".format(i_input))
22
+ return
23
+ image = ~image
24
+ cv2.imwrite(i_output, image)
25
+
26
+ def pdf_to_img_all(self, file_path, o_dir):
27
+ """Converts all PDF pages to JPEG images."""
28
+ if not os.path.exists(o_dir):
29
+ os.makedirs(o_dir)
30
+ pages = convert_from_path(file_path, dpi=self.dpi)
31
+ for i, image in enumerate(pages):
32
+ output_path = os.path.join(o_dir, f"{i+1}.jpeg")
33
+ image.save(output_path, 'JPEG', quality=95)
34
+ print("Saved image: {}".format(output_path))
35
+
36
+ def get_scaled_dimensions(self, width_pixels, height_pixels):
37
+ """Calculate scaled dimensions maintaining aspect ratio."""
38
+ width_ratio = width_pixels / height_pixels
39
+
40
+ if width_ratio > 1: # Landscape
41
+ w = self.a4_h_mm
42
+ h = self.a4_h_mm / width_ratio
43
+ return 'L', w, h
44
+ else: # Portrait
45
+ h = self.a4_h_mm
46
+ w = self.a4_h_mm * width_ratio
47
+ return 'P', w, h
48
+
49
+ def img_to_pdf(self, i_dir, o_dir, filename, invert_pages):
50
+ """Combines images into PDF, inverting specified pages."""
51
+ pdf = FPDF(unit="mm", format='A4')
52
+ pdf.set_auto_page_break(auto=False, margin=0)
53
+ pdf.set_margins(0, 0, 0)
54
+
55
+ # Collect and sort image files
56
+ filepaths = []
57
+ for filepath in glob.iglob(os.path.join(i_dir, '*.jpeg')):
58
+ filepaths.append(filepath)
59
+
60
+ pages = []
61
+ for path in filepaths:
62
+ base = os.path.basename(path)
63
+ match = re.search(r'(\d+)\.jpeg', base)
64
+ if match:
65
+ pages.append((int(match.group(1)), path))
66
+ pages.sort(key=lambda x: x[0])
67
+
68
+ # Process each page
69
+ for page_num, img_path in pages:
70
+ if page_num in invert_pages:
71
+ self.invert_image(img_path, img_path)
72
+ else:
73
+ print("Keeping original image: {}".format(img_path))
74
+
75
+ with PILImage.open(img_path) as img:
76
+ width_pixels, height_pixels = img.size
77
+
78
+ # Get scaled dimensions and orientation
79
+ orientation, w, h = self.get_scaled_dimensions(width_pixels, height_pixels)
80
+
81
+ # Add page with proper orientation
82
+ pdf.add_page(orientation=orientation)
83
+
84
+ # Center image on page
85
+ x = (self.a4_w_mm if orientation == 'P' else self.a4_h_mm - w) / 2
86
+ y = (self.a4_h_mm if orientation == 'P' else self.a4_w_mm - h) / 2
87
+
88
+ # Place image
89
+ pdf.image(img_path, x=x, y=y, w=w, h=h)
90
+ print(f"Added {img_path} to PDF ({orientation})")
91
+
92
+ # Save output
93
+ if not os.path.exists(o_dir):
94
+ os.makedirs(o_dir)
95
+ output_pdf = os.path.join(o_dir, filename)
96
+ pdf.output(output_pdf, "F")
97
+ print("Generated PDF: {}".format(output_pdf))
98
+
99
+
100
+ def parse_page_ranges(range_str):
101
+ """Converts range string to list of page numbers."""
102
+ pages = set()
103
+ parts = range_str.split(',')
104
+ for part in parts:
105
+ part = part.strip()
106
+ if '-' in part:
107
+ start, end = part.split('-')
108
+ pages.update(range(int(start), int(end) + 1))
109
+ else:
110
+ pages.add(int(part))
111
+ return sorted(pages)
112
+
113
+
114
+ if __name__ == "__main__":
115
+ converter = Converter()
116
+ pdf_file = 'input.pdf'
117
+ img_dir = 'images'
118
+ output_dir = 'output'
119
+ output_pdf_name = 'result.pdf'
120
+
121
+ page_range_str = "1-12,14-20,22-32,56,66-78,82-97"
122
+ pages_to_invert = parse_page_ranges(page_range_str)
123
+
124
+ converter.pdf_to_img_all(pdf_file, img_dir)
125
+ converter.img_to_pdf(img_dir, output_dir, output_pdf_name, pages_to_invert)
main2.py ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import fitz
2
+ import numpy as np
3
+
4
+ def parse_page_selection(selection):
5
+ pages = set()
6
+ for part in selection.split(','):
7
+ if '-' in part:
8
+ start, end = map(int, part.split('-'))
9
+ pages.update(range(start - 1, end)) # Convert to 0-based index
10
+ else:
11
+ pages.add(int(part) - 1) # Convert to 0-based index
12
+ return sorted(pages)
13
+
14
+ def invert_colors(pix):
15
+ img = np.frombuffer(pix.samples, dtype=np.uint8)
16
+ img = 255 - img # Invert colors
17
+ return fitz.Pixmap(pix.colorspace, pix.width, pix.height, img.tobytes(), pix.alpha)
18
+
19
+ def invert_pdf_pages(input_pdf, output_pdf, page_selection):
20
+ pages_to_invert = parse_page_selection(page_selection)
21
+ doc = fitz.open(input_pdf)
22
+
23
+ for page_num in pages_to_invert:
24
+ if 0 <= page_num < len(doc):
25
+ page = doc[page_num]
26
+ pix = page.get_pixmap()
27
+ inverted_pix = invert_colors(pix)
28
+ img_rect = page.rect
29
+ page.clean_contents()
30
+ page.insert_image(img_rect, stream=inverted_pix.tobytes())
31
+
32
+ doc.save(output_pdf)
33
+ doc.close()
34
+
35
+ # Usage example
36
+ invert_pdf_pages("input.pdf", "output.pdf", "1-12,14-20,22-32,56,66-78,82-97")
requirements.py ADDED
File without changes
run.sh ADDED
File without changes