5
5
"""
6
6
7
7
import os
8
+ import sys
9
+ import shutil
8
10
import tempfile
11
+ import subprocess
9
12
from pathlib import Path
10
13
from typing import Dict , List , Optional
11
14
from fsspec import AbstractFileSystem
@@ -56,9 +59,52 @@ def __init__(self) -> None:
56
59
"tokenizer" : tokenizer ,
57
60
}
58
61
62
+ def find_libreoffice (self ) -> str :
63
+ """Finds the LibreOffice executable path."""
64
+ libreoffice_path = shutil .which ("soffice" )
65
+
66
+ if not libreoffice_path and sys .platform == "win32" :
67
+ # Check common installation paths on Windows
68
+ possible_paths = [
69
+ r"C:\Program Files\LibreOffice\program\soffice.exe" ,
70
+ r"C:\Program Files (x86)\LibreOffice\program\soffice.exe" ,
71
+ ]
72
+ libreoffice_path = next (
73
+ (path for path in possible_paths if os .path .exists (path )), None
74
+ )
75
+
76
+ if not libreoffice_path :
77
+ raise OSError (
78
+ "LibreOffice (soffice) not found. Please install LibreOffice or add it to your system PATH."
79
+ )
80
+
81
+ return libreoffice_path
82
+
83
+ def convert_wmf_to_png (self , input_path : str ) -> str :
84
+ """Convert WMF/EMF to PNG using LibreOffice."""
85
+ file_path = Path (input_path )
86
+ output_path = file_path .with_suffix (".png" )
87
+
88
+ libreoffice_path = self .find_libreoffice ()
89
+
90
+ subprocess .run (
91
+ [
92
+ libreoffice_path ,
93
+ "--headless" ,
94
+ "--convert-to" ,
95
+ "png" ,
96
+ "--outdir" ,
97
+ str (file_path .parent ),
98
+ str (file_path ),
99
+ ],
100
+ check = True ,
101
+ )
102
+
103
+ return str (output_path )
104
+
59
105
def caption_image (self , tmp_image_file : str ) -> str :
60
106
"""Generate text caption of image."""
61
- from PIL import Image
107
+ from PIL import Image , UnidentifiedImageError
62
108
63
109
model = self .parser_config ["model" ]
64
110
feature_extractor = self .parser_config ["feature_extractor" ]
@@ -71,7 +117,20 @@ def caption_image(self, tmp_image_file: str) -> str:
71
117
num_beams = 4
72
118
gen_kwargs = {"max_length" : max_length , "num_beams" : num_beams }
73
119
74
- i_image = Image .open (tmp_image_file )
120
+ try :
121
+ i_image = Image .open (tmp_image_file )
122
+ image_format = i_image .format
123
+ except UnidentifiedImageError :
124
+ return "Error opening image file."
125
+
126
+ if image_format in ["WMF" , "EMF" ]:
127
+ try :
128
+ converted_path = self .convert_wmf_to_png (tmp_image_file )
129
+ i_image = Image .open (converted_path )
130
+ except Exception as e :
131
+ print (f"Error converting WMF/EMF image: { e } " )
132
+ return f"Error converting WMF/EMF image"
133
+
75
134
if i_image .mode != "RGB" :
76
135
i_image = i_image .convert (mode = "RGB" )
77
136
0 commit comments