From bc1b837616223906b0003361a9c6a52ab3a85ec4 Mon Sep 17 00:00:00 2001 From: WuWeiFlow <83688074+WuWeiFlow@users.noreply.github.com> Date: Tue, 24 Jun 2025 18:01:13 +0800 Subject: [PATCH] =?UTF-8?q?FIX:Saving=20an=20RGBA=20image=20directly=20as?= =?UTF-8?q?=20JPEG=20will=20cause=20an=20error.=20If=20the=E2=80=A6=20(#83?= =?UTF-8?q?99)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Saving an RGBA image directly as JPEG will cause an error. If the image is in RGBA mode, convert it to RGB mode before saving it in JPG format. ### What problem does this PR solve? During document parsing in the knowledge base, we occasionally encounter the error 'cannot write mode RGBA as JPEG.' This occurs because images in RGBA mode cannot be directly saved as JPEG. They must be converted first before saving. ### Type of change - [x] Bug Fix (non-breaking change which fixes an issue) --- rag/svr/task_executor.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/rag/svr/task_executor.py b/rag/svr/task_executor.py index e5fed0bea..b028f7125 100644 --- a/rag/svr/task_executor.py +++ b/rag/svr/task_executor.py @@ -293,6 +293,9 @@ async def build_chunks(task, progress_callback): if isinstance(d["image"], bytes): output_buffer = BytesIO(d["image"]) else: + # If the image is in RGBA mode, convert it to RGB mode before saving it in JPEG format. + if d["image"].mode in ("RGBA", "P"): + d["image"] = d["image"].convert("RGB") d["image"].save(output_buffer, format='JPEG') async with minio_limiter: await trio.to_thread.run_sync(lambda: STORAGE_IMPL.put(task["kb_id"], d["id"], output_buffer.getvalue()))