|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
3 | | -import io |
4 | 3 | from pathlib import Path |
5 | | -from typing import Any |
| 4 | +from typing import Any, BinaryIO |
6 | 5 |
|
7 | 6 | from mindee.dependencies.checkers import PILLOW_AVAILABLE |
8 | 7 | from mindee.dependencies.decorators import requires_pillow |
9 | 8 | from mindee.error.mindee_error import MindeeError |
10 | | -from mindee.input.file_input import FileInput |
11 | | -from mindee.input.local_input_source import LocalInputSource |
| 9 | +from mindee.input.bytes_input import BytesInput |
12 | 10 | from mindee.logger import logger |
13 | 11 |
|
14 | 12 | if PILLOW_AVAILABLE: |
|
21 | 19 | class ExtractedImage: |
22 | 20 | """Generic class for image extraction.""" |
23 | 21 |
|
| 22 | + buffer: BinaryIO |
| 23 | + filename: str |
24 | 24 | _page_id: int |
25 | 25 | """Id of the page the image was extracted from.""" |
26 | 26 | _element_id: int |
27 | 27 | """Id of the element on a given page.""" |
28 | | - filename: str |
29 | | - """Name of the file the image was extracted from.""" |
30 | 28 |
|
31 | 29 | def __init__( |
32 | | - self, input_source: LocalInputSource, page_id: int, element_id: int |
| 30 | + self, |
| 31 | + img_byte_stream: BinaryIO, |
| 32 | + filename: str, |
| 33 | + page_id: int, |
| 34 | + element_id: int, |
33 | 35 | ) -> None: |
34 | 36 | """ |
35 | 37 | Initialize the ExtractedImage with a buffer and an internal file name. |
36 | 38 |
|
37 | | - :param input_source: Local source for input. |
| 39 | + :param img_byte_stream: The raw image bytes. |
| 40 | + :param filename: Name of the file. |
38 | 41 | :param page_id: ID of the page the element was found on. |
39 | 42 | :param element_id: ID of the element in a page. |
40 | 43 | """ |
41 | | - self.buffer = io.BytesIO(input_source.file_object.read()) |
42 | | - self.buffer.name = input_source.filename |
43 | | - self.filename = input_source.filename |
44 | | - if input_source.is_pdf(): |
45 | | - extension = "jpg" |
46 | | - else: |
47 | | - extension = Path(input_source.filename).resolve().suffix |
| 44 | + self.buffer = img_byte_stream |
48 | 45 | self.buffer.seek(0) |
49 | | - pg_number = str(page_id).zfill(3) |
50 | | - elem_number = str(element_id).zfill(3) |
51 | | - self.internal_file_name = ( |
52 | | - f"{input_source.filename}_page{pg_number}-{elem_number}.{extension}" |
53 | | - ) |
| 46 | + self.filename = filename |
54 | 47 | self._page_id = page_id |
55 | 48 | self._element_id = 0 if element_id is None else element_id |
56 | 49 |
|
57 | 50 | @requires_pillow |
58 | | - def save_to_file(self, output_path: Path | str, file_format: str | None = None): |
| 51 | + def save_to_file(self, output_path: Path | str): |
59 | 52 | """ |
60 | 53 | Saves the document to a file. |
61 | 54 |
|
62 | 55 | :param output_path: Path to save the file to. |
63 | | - :param file_format: Optional PIL-compatible format for the file. Inferred from file extension if not provided. |
64 | 56 | :raises MindeeError: If an invalid path or filename is provided. |
65 | 57 | """ |
| 58 | + out_path = Path(output_path) |
| 59 | + if not out_path.resolve().is_dir(): |
| 60 | + raise MindeeError("Provided path is not a directory.") |
| 61 | + out_file_path = out_path / self.filename |
66 | 62 | try: |
67 | | - resolved_path = Path(output_path).resolve() |
68 | | - if not file_format and len(resolved_path.suffix) < 1: |
69 | | - raise ValueError("Invalid file format.") |
70 | 63 | self.buffer.seek(0) |
71 | 64 | image = Image.open(self.buffer) |
72 | | - if file_format: |
73 | | - image.save(resolved_path, format=file_format) |
74 | | - else: |
75 | | - image.save(resolved_path) |
76 | | - logger.info("File saved successfully to '%s'.", resolved_path) |
77 | | - except TypeError as e: |
78 | | - raise MindeeError("Invalid path/filename provided.") from e |
| 65 | + image.save(out_file_path) |
| 66 | + logger.info("File saved successfully to '%s'.", out_file_path) |
79 | 67 | except Exception as e: |
80 | 68 | print(e) |
81 | 69 | raise MindeeError(f"Could not save file {Path(output_path).name}.") from e |
82 | 70 |
|
83 | | - def as_input_source(self) -> FileInput: |
| 71 | + def as_input_source(self) -> BytesInput: |
84 | 72 | """ |
85 | 73 | Return the file as a Mindee-compatible BufferInput source. |
86 | 74 |
|
87 | 75 | :returns: A BufferInput source. |
88 | 76 | """ |
89 | 77 | self.buffer.seek(0) |
90 | | - return FileInput(self.buffer) |
| 78 | + return BytesInput(self.buffer.read(), self.filename) |
91 | 79 |
|
92 | 80 | @property |
93 | 81 | def page_id(self): |
94 | 82 | """ |
95 | | - ID of the page the receipt was found on. |
| 83 | + ID of the page the image was found on. |
96 | 84 |
|
97 | 85 | :return: A valid page ID. |
98 | 86 | """ |
|
0 commit comments