|
12 | 12 | from mindee.input.sources.local_input_source import LocalInputSource
|
13 | 13 |
|
14 | 14 |
|
| 15 | +def attach_image_as_new_file( # type: ignore |
| 16 | + input_buffer: BinaryIO, |
| 17 | +) -> pdfium.PdfDocument: |
| 18 | + """ |
| 19 | + Attaches an image as a new page in a PdfDocument object. |
| 20 | +
|
| 21 | + :param input_buffer: Input buffer. |
| 22 | + :return: A PdfDocument handle. |
| 23 | + """ |
| 24 | + # Create a new page in the PdfDocument |
| 25 | + input_buffer.seek(0) |
| 26 | + image = Image.open(input_buffer) |
| 27 | + image.convert("RGB") |
| 28 | + image_buffer = io.BytesIO() |
| 29 | + image.save(image_buffer, format="JPEG") |
| 30 | + |
| 31 | + pdf = pdfium.PdfDocument.new() |
| 32 | + |
| 33 | + image_pdf = pdfium.PdfImage.new(pdf) |
| 34 | + image_pdf.load_jpeg(image_buffer) |
| 35 | + width, height = image_pdf.get_size() |
| 36 | + |
| 37 | + matrix = pdfium.PdfMatrix().scale(width, height) |
| 38 | + image_pdf.set_matrix(matrix) |
| 39 | + |
| 40 | + page = pdf.new_page(width, height) |
| 41 | + page.insert_obj(image_pdf) |
| 42 | + page.gen_content() |
| 43 | + image.close() |
| 44 | + return pdf |
| 45 | + |
| 46 | + |
15 | 47 | def extract_image_from_polygon(
|
16 | 48 | page_content: Image.Image,
|
17 | 49 | polygon: List[Point],
|
@@ -129,35 +161,4 @@ def load_pdf_doc(input_file: LocalInputSource) -> pdfium.PdfDocument: # type: i
|
129 | 161 | input_file.file_object.seek(0)
|
130 | 162 | return pdfium.PdfDocument(input_file.file_object.read())
|
131 | 163 |
|
132 |
| - return attach_images_as_new_file([input_file.file_object]) |
133 |
| - |
134 |
| - |
135 |
| -def attach_images_as_new_file( # type: ignore |
136 |
| - input_buffer_list: List[BinaryIO], |
137 |
| -) -> pdfium.PdfDocument: |
138 |
| - """ |
139 |
| - Attaches a list of images as new pages in a PdfDocument object. |
140 |
| -
|
141 |
| - :param input_buffer_list: List of images, represented as buffers. |
142 |
| - :return: A PdfDocument handle. |
143 |
| - """ |
144 |
| - pdf = pdfium.PdfDocument.new() |
145 |
| - for input_buffer in input_buffer_list: |
146 |
| - input_buffer.seek(0) |
147 |
| - image = Image.open(input_buffer) |
148 |
| - image.convert("RGB") |
149 |
| - image_buffer = io.BytesIO() |
150 |
| - image.save(image_buffer, format="JPEG") |
151 |
| - |
152 |
| - image_pdf = pdfium.PdfImage.new(pdf) |
153 |
| - image_pdf.load_jpeg(image_buffer) |
154 |
| - width, height = image_pdf.get_size() |
155 |
| - |
156 |
| - matrix = pdfium.PdfMatrix().scale(width, height) |
157 |
| - image_pdf.set_matrix(matrix) |
158 |
| - |
159 |
| - page = pdf.new_page(width, height) |
160 |
| - page.insert_obj(image_pdf) |
161 |
| - page.gen_content() |
162 |
| - image.close() |
163 |
| - return pdf |
| 164 | + return attach_image_as_new_file(input_file.file_object) |
0 commit comments