11import io
22from pathlib import Path
3- from typing import Optional
3+ from typing import Optional , Union
44
55from PIL import Image
66
@@ -17,6 +17,8 @@ class ExtractedImage:
1717 """Id of the page the image was extracted from."""
1818 _element_id : int
1919 """Id of the element on a given page."""
20+ filename : str
21+ """Name of the file the image was extracted from."""
2022
2123 def __init__ (
2224 self , input_source : LocalInputSource , page_id : int , element_id : int
@@ -30,6 +32,7 @@ def __init__(
3032 """
3133 self .buffer = io .BytesIO (input_source .file_object .read ())
3234 self .buffer .name = input_source .filename
35+ self .filename = input_source .filename
3336 if input_source .is_pdf ():
3437 extension = "jpg"
3538 else :
@@ -43,7 +46,9 @@ def __init__(
4346 self ._page_id = page_id
4447 self ._element_id = 0 if element_id is None else element_id
4548
46- def save_to_file (self , output_path : str , file_format : Optional [str ] = None ):
49+ def save_to_file (
50+ self , output_path : Union [Path , str ], file_format : Optional [str ] = None
51+ ):
4752 """
4853 Saves the document to a file.
4954
@@ -56,20 +61,27 @@ def save_to_file(self, output_path: str, file_format: Optional[str] = None):
5661 if not file_format :
5762 if len (resolved_path .suffix ) < 1 :
5863 raise ValueError ("Invalid file format." )
59- file_format = (
60- resolved_path .suffix .upper ()
61- ) # technically redundant since PIL applies an upper operation
62- # to the parameter , but older versions may not do so.
64+ # Let PIL infer format from filename extension
6365 self .buffer .seek (0 )
6466 image = Image .open (self .buffer )
65- image .save (resolved_path , format = file_format )
67+ if file_format :
68+ image .save (resolved_path , format = file_format )
69+ else :
70+ image .save (resolved_path )
6671 logger .info ("File saved successfully to '%s'." , resolved_path )
6772 except TypeError as exc :
6873 raise MindeeError ("Invalid path/filename provided." ) from exc
6974 except Exception as exc :
75+ print (exc )
7076 raise MindeeError (f"Could not save file { Path (output_path ).name } ." ) from exc
7177
7278 def as_source (self ) -> FileInput :
79+ """
80+ Deprecated. Use ``as_input_source`` instead.
81+ """
82+ return self .as_input_source ()
83+
84+ def as_input_source (self ) -> FileInput :
7385 """
7486 Return the file as a Mindee-compatible BufferInput source.
7587
0 commit comments