Skip to content

Commit f973b23

Browse files
committed
other fixes
1 parent 481421a commit f973b23

7 files changed

Lines changed: 36 additions & 38 deletions

File tree

mindee/image/__init__.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +0,0 @@
1-
from mindee.image.image_compressor import compress_image
2-
3-
__all__ = ["compress_image"]

mindee/image/extracted_image.py

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
from __future__ import annotations
22

3-
import io
43
from pathlib import Path
5-
from typing import Any
4+
from typing import Any, BinaryIO
65

76
from mindee.dependencies.checkers import PILLOW_AVAILABLE
87
from mindee.dependencies.decorators import requires_pillow
98
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
1210
from mindee.logger import logger
1311

1412
if PILLOW_AVAILABLE:
@@ -21,6 +19,7 @@
2119
class ExtractedImage:
2220
"""Generic class for image extraction."""
2321

22+
buffer: BinaryIO
2423
_page_id: int
2524
"""Id of the page the image was extracted from."""
2625
_element_id: int
@@ -29,27 +28,33 @@ class ExtractedImage:
2928
"""Name of the file the image was extracted from."""
3029

3130
def __init__(
32-
self, input_source: LocalInputSource, page_id: int, element_id: int
31+
self,
32+
img_byte_stream: BinaryIO,
33+
orig_filename: str,
34+
orig_extension: str,
35+
page_id: int,
36+
element_id: int,
3337
) -> None:
3438
"""
3539
Initialize the ExtractedImage with a buffer and an internal file name.
3640
37-
:param input_source: Local source for input.
41+
:param img_byte_stream: The raw image bytes.
42+
:param orig_filename: Name of the file the image was extracted from.
3843
:param page_id: ID of the page the element was found on.
3944
:param element_id: ID of the element in a page.
4045
"""
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():
46+
self.buffer = img_byte_stream
47+
self.filename = orig_filename
48+
49+
if orig_extension.lower().endswith("pdf"):
4550
extension = "jpg"
4651
else:
47-
extension = Path(input_source.filename).resolve().suffix
52+
extension = orig_extension.lower()
4853
self.buffer.seek(0)
4954
pg_number = str(page_id).zfill(3)
5055
elem_number = str(element_id).zfill(3)
5156
self.internal_file_name = (
52-
f"{input_source.filename}_page{pg_number}-{elem_number}.{extension}"
57+
f"{orig_filename}_page{pg_number}-{elem_number}.{extension}"
5358
)
5459
self._page_id = page_id
5560
self._element_id = 0 if element_id is None else element_id
@@ -76,14 +81,14 @@ def save_to_file(self, output_path: Path | str):
7681
print(e)
7782
raise MindeeError(f"Could not save file {Path(output_path).name}.") from e
7883

79-
def as_input_source(self) -> FileInput:
84+
def as_input_source(self) -> BytesInput:
8085
"""
8186
Return the file as a Mindee-compatible BufferInput source.
8287
8388
:returns: A BufferInput source.
8489
"""
8590
self.buffer.seek(0)
86-
return FileInput(self.buffer)
91+
return BytesInput(self.buffer.read(), self.internal_file_name)
8792

8893
@property
8994
def page_id(self):

mindee/image/image_extractor.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
from mindee.geometry.point import Point
1111
from mindee.geometry.polygon import Polygon, get_min_max_x, get_min_max_y
1212
from mindee.image.extracted_image import ExtractedImage
13-
from mindee.input.bytes_input import BytesInput
1413
from mindee.input.local_input_source import LocalInputSource
1514

1615
if PYPDFIUM2_AVAILABLE:
@@ -66,7 +65,7 @@ def extract_image_from_polygon(
6665
width: float,
6766
height: float,
6867
file_format: str,
69-
) -> bytes:
68+
) -> BinaryIO:
7069
"""
7170
Crops the image from the given polygon.
7271
@@ -91,7 +90,7 @@ def extract_image_from_polygon(
9190

9291

9392
@requires_pillow
94-
def save_image_to_buffer(image: Image.Image, file_format: str) -> bytes:
93+
def save_image_to_buffer(image: Image.Image, file_format: str) -> BinaryIO:
9594
"""
9695
Saves an image as a buffer.
9796
@@ -102,7 +101,7 @@ def save_image_to_buffer(image: Image.Image, file_format: str) -> bytes:
102101
buffer = io.BytesIO()
103102
image.save(buffer, format=file_format)
104103
buffer.seek(0)
105-
return buffer.read()
104+
return buffer
106105

107106

108107
@requires_pillow
@@ -159,10 +158,9 @@ def extract_multiple_images_from_source(
159158
)
160159
extracted_elements.append(
161160
ExtractedImage(
162-
BytesInput(
163-
image_data,
164-
f"{input_source.filename}_page{page_id + 1}-{element_id}.{file_extension}",
165-
),
161+
image_data,
162+
input_source.filename,
163+
file_extension,
166164
page_id,
167165
element_id,
168166
)

mindee/input/local_input_source.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from mindee.dependencies.checkers import PYPDFIUM2_AVAILABLE
1111
from mindee.error.mimetype_error import MimeTypeError
1212
from mindee.error.mindee_error import MindeeError, MindeeSourceError
13-
from mindee.image import compress_image
13+
from mindee.image.image_compressor import compress_image
1414
from mindee.input.page_options import KEEP_ONLY, REMOVE, PageOptions
1515
from mindee.logger import logger
1616
from mindee.pdf.pdf_compressor import compress_pdf

mindee/pdf/extracted_pdf.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,18 @@
1818
class ExtractedPDF:
1919
"""An extracted sub-Pdf."""
2020

21-
pdf_bytes: BinaryIO
21+
buffer: BinaryIO
2222
filename: str
2323

24-
def __init__(self, pdf_bytes: BinaryIO, filename: str):
25-
self.pdf_bytes = pdf_bytes
24+
def __init__(self, pdf_byte_stream: BinaryIO, filename: str):
25+
self.buffer = pdf_byte_stream
2626
self.filename = filename
2727

2828
@requires_pypdfium2
2929
def get_page_count(self) -> int:
3030
"""Get the number of pages in the PDF file."""
3131
try:
32-
pdf = pdfium.PdfDocument(self.pdf_bytes)
32+
pdf = pdfium.PdfDocument(self.buffer)
3333
return len(pdf)
3434
except Exception as e:
3535
raise MindeeError(
@@ -50,11 +50,11 @@ def save_to_file(self, output_path: Path | str):
5050
raise MindeeError("Invalid save path provided {}.")
5151
if out_path.suffix.lower() != "pdf":
5252
out_path = out_path.parent / (out_path.stem + "." + "pdf")
53-
self.pdf_bytes.seek(0)
53+
self.buffer.seek(0)
5454
with open(out_path, "wb") as out_file:
55-
out_file.write(self.pdf_bytes.read())
55+
out_file.write(self.buffer.read())
5656

5757
def as_input_source(self) -> BytesInput:
5858
"""Returns the current PDF object as a usable BytesInput source."""
59-
self.pdf_bytes.seek(0)
60-
return BytesInput(self.pdf_bytes.read(), self.filename)
59+
self.buffer.seek(0)
60+
return BytesInput(self.buffer.read(), self.filename)

tests/input/test_compression.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
import pytest
88

9-
from mindee.image import compress_image
9+
from mindee.image.image_compressor import compress_image
1010
from mindee.input import PathInput
1111
from mindee.pdf.pdf_compressor import compress_pdf
1212
from mindee.pdf.pdf_utils import extract_text_from_pdf

tests/v1/extraction/test_invoice_splitter_auto_extraction.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,7 @@ def test_pdf_should_extract_invoices_strict():
5353
)
5454
for i, extracted_pdf in enumerate(extracted_base_pdfs):
5555
assert extracted_pdf.filename == extracted_pdfs_strict[i].filename
56-
assert (
57-
extracted_pdf.pdf_bytes.read() == extracted_pdfs_strict[i].pdf_bytes.read()
58-
)
56+
assert extracted_pdf.buffer.read() == extracted_pdfs_strict[i].buffer.read()
5957

6058
assert len(extracted_pdfs_not_strict) == 2
6159
assert extracted_pdfs_not_strict[0].filename == "default_sample_001-001.pdf"

0 commit comments

Comments
 (0)