CoolFace
Apppublic

Aqdas/CV-Extractor

sourceHugging Faceupdated 2y agoView on Hugging Face
2likes
pdf_to_image.py23 linesDownload Raw Back to root
1import fitz  # PyMuPDF2from PIL import Image3 4def pdf_to_image(pdf_files, dpi=300):5    pdf_images = []6    for pdf_file in pdf_files:7        pdf_bytes = pdf_file.read()  # Read the uploaded file as bytes8        pdf_document = fitz.open(stream=pdf_bytes, filetype="pdf")9        images = []10        for page_num in range(len(pdf_document)):11            page = pdf_document.load_page(page_num)12            zoom = dpi / 72  # 72 is the default DPI of the PDF13            mat = fitz.Matrix(zoom, zoom)14            pix = page.get_pixmap(matrix=mat)15            img = Image.frombytes("RGB", [pix.width, pix.height], pix.samples)16            images.append(img)17            18        pdf_images.append(images)19    return pdf_images20 21 22 23