add RGB conversion

Signed-off-by: Christoph Auer <cau@zurich.ibm.com>
This commit is contained in:
Christoph Auer 2025-07-11 10:26:43 +02:00
parent 6aa85cc933
commit 05f51b30d9

View File

@ -57,28 +57,31 @@ class PdfDocumentBackend(PaginatedDocumentBackend):
if self.input_format is InputFormat.IMAGE:
buf = BytesIO()
img = Image.open(self.path_or_stream)
# Handle multi-page TIFF images
if hasattr(img, 'n_frames') and img.n_frames > 1:
if hasattr(img, "n_frames") and img.n_frames > 1:
# Extract all frames from multi-page image
frames = []
try:
for i in range(img.n_frames):
img.seek(i)
frames.append(img.copy())
frame = img.copy().convert("RGB")
frames.append(frame)
except EOFError:
pass
# Save as multi-page PDF
if frames:
frames[0].save(buf, "PDF", save_all=True, append_images=frames[1:])
frames[0].save(
buf, "PDF", save_all=True, append_images=frames[1:]
)
else:
# Fallback to single page if frame extraction fails
img.save(buf, "PDF")
img.convert("RGB").save(buf, "PDF")
else:
# Single page image - use existing behavior
img.save(buf, "PDF")
# Single page image - convert to RGB and save
img.convert("RGB").save(buf, "PDF")
buf.seek(0)
self.path_or_stream = buf
else: