55"""
66
77import os
8+ import sys
9+ import shutil
810import tempfile
11+ import subprocess
912from pathlib import Path
1013from typing import Dict , List , Optional
1114from fsspec import AbstractFileSystem
@@ -56,9 +59,52 @@ def __init__(self) -> None:
5659 "tokenizer" : tokenizer ,
5760 }
5861
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+
59105 def caption_image (self , tmp_image_file : str ) -> str :
60106 """Generate text caption of image."""
61- from PIL import Image
107+ from PIL import Image , UnidentifiedImageError
62108
63109 model = self .parser_config ["model" ]
64110 feature_extractor = self .parser_config ["feature_extractor" ]
@@ -71,7 +117,20 @@ def caption_image(self, tmp_image_file: str) -> str:
71117 num_beams = 4
72118 gen_kwargs = {"max_length" : max_length , "num_beams" : num_beams }
73119
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+
75134 if i_image .mode != "RGB" :
76135 i_image = i_image .convert (mode = "RGB" )
77136
0 commit comments