|
| 1 | +import io |
| 2 | +from typing import BinaryIO, List |
| 3 | + |
| 4 | +import pypdfium2 as pdfium |
| 5 | +from PIL import Image |
| 6 | + |
| 7 | +from mindee.geometry import Point, get_min_max_x, get_min_max_y |
| 8 | +from mindee.image_extraction.common import ExtractedImage |
| 9 | +from mindee.input import BytesInput, LocalInputSource |
| 10 | + |
| 11 | + |
| 12 | +def attach_image_as_new_file( # type: ignore |
| 13 | + input_buffer: BinaryIO, |
| 14 | +) -> pdfium.PdfDocument: |
| 15 | + """ |
| 16 | + Attaches an image as a new page in a PdfDocument object. |
| 17 | +
|
| 18 | + :param input_buffer: Input buffer. Only supports JPEG. |
| 19 | + :return: A PdfDocument handle. |
| 20 | + """ |
| 21 | + # Create a new page in the PdfDocument |
| 22 | + input_buffer.seek(0) |
| 23 | + image = Image.open(input_buffer) |
| 24 | + image.convert("RGB") |
| 25 | + image_buffer = io.BytesIO() |
| 26 | + image.save(image_buffer, format="JPEG") |
| 27 | + |
| 28 | + pdf = pdfium.PdfDocument.new() |
| 29 | + |
| 30 | + image_pdf = pdfium.PdfImage.new(pdf) |
| 31 | + image_pdf.load_jpeg(image_buffer) |
| 32 | + width, height = image_pdf.get_size() |
| 33 | + |
| 34 | + matrix = pdfium.PdfMatrix().scale(width, height) |
| 35 | + image_pdf.set_matrix(matrix) |
| 36 | + |
| 37 | + page = pdf.new_page(width, height) |
| 38 | + page.insert_obj(image_pdf) |
| 39 | + page.gen_content() |
| 40 | + image.close() |
| 41 | + return pdf |
| 42 | + |
| 43 | + |
| 44 | +def extract_multiple_images_from_source( |
| 45 | + input_source: LocalInputSource, page_id: int, polygons: List[List[Point]] |
| 46 | +) -> List[ExtractedImage]: |
| 47 | + """ |
| 48 | + Extracts elements from a page based on a list of bounding boxes. |
| 49 | +
|
| 50 | + :param input_source: Local Input source to extract elements from. |
| 51 | + :param page_id: id of the page to extract from. |
| 52 | + :param polygons: List of coordinates to pull the elements from. |
| 53 | + :return: List of byte arrays representing the extracted elements. |
| 54 | + """ |
| 55 | + page = load_pdf_doc(input_source).get_page(page_id) |
| 56 | + page_content = page.render().to_pil() |
| 57 | + width, height = page.get_size() |
| 58 | + |
| 59 | + extracted_elements = [] |
| 60 | + for element_id, polygon in enumerate(polygons): |
| 61 | + min_max_x = get_min_max_x(polygon) |
| 62 | + min_max_y = get_min_max_y(polygon) |
| 63 | + |
| 64 | + pillow_page = page_content.crop( |
| 65 | + ( |
| 66 | + int(min_max_x.min * width), |
| 67 | + int(min_max_y.min * height), |
| 68 | + int(min_max_x.max * width), |
| 69 | + int(min_max_y.max * height), |
| 70 | + ) |
| 71 | + ) |
| 72 | + buffer = io.BytesIO() |
| 73 | + pillow_page.save(buffer, format="JPEG") |
| 74 | + buffer.seek(0) |
| 75 | + extracted_elements.append( |
| 76 | + ExtractedImage( |
| 77 | + BytesInput( |
| 78 | + buffer.read(), |
| 79 | + f"{input_source.filename}_p{page_id}_e{element_id}.jpg", |
| 80 | + ), |
| 81 | + page_id, |
| 82 | + element_id, |
| 83 | + ) |
| 84 | + ) |
| 85 | + |
| 86 | + return extracted_elements |
| 87 | + |
| 88 | + |
| 89 | +def load_pdf_doc(input_file: LocalInputSource) -> pdfium.PdfDocument: # type: ignore |
| 90 | + """ |
| 91 | + Loads a PDF document from a local input source. |
| 92 | +
|
| 93 | + :param input_file: Local input. |
| 94 | + :return: A valid PdfDocument handle. |
| 95 | + """ |
| 96 | + if input_file.is_pdf(): |
| 97 | + input_file.file_object.seek(0) |
| 98 | + return pdfium.PdfDocument(input_file.file_object) |
| 99 | + |
| 100 | + return attach_image_as_new_file(input_file.file_object) |
0 commit comments