changes
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
"""Helper: turn an ir.attachment into a list of PIL.Image pages.
|
||||
|
||||
Kept separate from the adapters so future backends (Ollama-vision, Mindee)
|
||||
that want PIL images directly don't have to re-implement the PDF rendering.
|
||||
"""
|
||||
|
||||
import base64
|
||||
import io
|
||||
import logging
|
||||
|
||||
_logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def attachment_to_pages(attachment):
|
||||
"""Decode an ir.attachment into a list of PIL.Image pages.
|
||||
|
||||
Returns ``[]`` on failure (caller should treat as no pages).
|
||||
"""
|
||||
try:
|
||||
from PIL import Image
|
||||
from pdf2image import convert_from_bytes
|
||||
except ImportError as e:
|
||||
_logger.warning("attachment_to_pages requires PIL + pdf2image: %s", e)
|
||||
return []
|
||||
|
||||
if not attachment or not attachment.datas:
|
||||
return []
|
||||
|
||||
try:
|
||||
data = base64.b64decode(attachment.datas)
|
||||
except Exception as e:
|
||||
_logger.warning("Could not decode attachment %s: %s", attachment.id, e)
|
||||
return []
|
||||
|
||||
mimetype = attachment.mimetype or ''
|
||||
is_pdf = mimetype == 'application/pdf' or data[:4] == b'%PDF'
|
||||
try:
|
||||
if is_pdf:
|
||||
return convert_from_bytes(data, dpi=200)
|
||||
return [Image.open(io.BytesIO(data))]
|
||||
except Exception as e:
|
||||
_logger.warning("Could not render attachment %s: %s", attachment.id, e)
|
||||
return []
|
||||
Reference in New Issue
Block a user