Skip to content

Commit

Permalink
Fix: Proper WMF/EMF image handling in PPTXReader
Browse files Browse the repository at this point in the history
  • Loading branch information
Leon-Sander committed Feb 14, 2025
1 parent 3080cf5 commit 749075e
Showing 1 changed file with 62 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,56 @@ def __init__(self) -> None:
"tokenizer": tokenizer,
}

def convert_wmf_to_png(self, input_path: str) -> str:
"""Convert WMF/EMF to PNG using LibreOffice, handling both Windows and Linux/macOS."""
import sys
import shutil
import subprocess
from pathlib import Path

file_path = Path(input_path)
output_path = file_path.with_suffix(".png")

if sys.platform == "win32":
libreoffice_path = r"C:\Program Files\LibreOffice\program\soffice.exe"
command = [
libreoffice_path,
"--headless",
"--convert-to",
"png",
str(file_path),
]
else:
libreoffice_path = shutil.which("libreoffice") or shutil.which("soffice")
command = [
"libreoffice",
"--headless",
"--convert-to",
"png",
"--outdir",
str(file_path.parent),
str(file_path),
]

if not libreoffice_path or not Path(libreoffice_path).exists():
raise RuntimeError(
f"LibreOffice is not installed. Cannot convert {file_path}."
)

subprocess.run(
command, check=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL
)

if not output_path.exists():
raise RuntimeError(
f"Conversion failed: {file_path}{output_path} does not exist."
)

return str(output_path)

def caption_image(self, tmp_image_file: str) -> str:
"""Generate text caption of image."""
from PIL import Image
from PIL import Image, UnidentifiedImageError

model = self.parser_config["model"]
feature_extractor = self.parser_config["feature_extractor"]
Expand All @@ -71,7 +118,20 @@ def caption_image(self, tmp_image_file: str) -> str:
num_beams = 4
gen_kwargs = {"max_length": max_length, "num_beams": num_beams}

i_image = Image.open(tmp_image_file)
try:
i_image = Image.open(tmp_image_file)
image_format = i_image.format
except UnidentifiedImageError:
return "Error opening image file."

if image_format in ["WMF", "EMF"]:
try:
converted_path = self.convert_wmf_to_png(tmp_image_file)
i_image = Image.open(converted_path)
except Exception as e:
print(f"Error converting WMF/EMF image: {e}")
return f"Error converting WMF/EMF image"

if i_image.mode != "RGB":
i_image = i_image.convert(mode="RGB")

Expand Down

0 comments on commit 749075e

Please sign in to comment.