24 lines
778 B
Python
24 lines
778 B
Python
# app/core/pdf_processor.py
|
||
from pdf2image import convert_from_bytes
|
||
from PIL import Image
|
||
from io import BytesIO
|
||
from typing import List
|
||
import base64
|
||
|
||
def convert_pdf_to_images(pdf_bytes: bytes) -> List[Image.Image]:
|
||
try:
|
||
print("--- [Core PDF] Converting PDF to images...")
|
||
|
||
images = convert_from_bytes(pdf_bytes)
|
||
|
||
print(f"--- [Core PDF] converted PDF to images,total {len(images)} pages。")
|
||
return images
|
||
except Exception as e:
|
||
print(f"--- [Core PDF] PDF conversion failed: {e}")
|
||
raise IOError(f"PDF to image conversion failed: {e}")
|
||
|
||
|
||
def image_to_base64_str(image: Image.Image) -> str:
|
||
buffered = BytesIO()
|
||
image.save(buffered, format="PNG")
|
||
return base64.b64encode(buffered.getvalue()).decode('utf-8') |