import fitz  # PyMuPDF
from PIL import Image
from PIL.Image import Resampling  # Importer Resampling directement

numpages = [1, 2, 3,4,5,6,9,12,14,16,20,23,27,33,39,44]
docPDF = "La proportionnalité au collège.pdf"

if __name__ == "__main__":
    counter = 1
    page_num = 0  # Numéro de page (0 pour la première page)
    dpi = 300  # Résolution DPI souhaitée
    pdf_document = fitz.open(docPDF)
    for num in numpages:
        page_num = num - 1
        page = pdf_document.load_page(page_num)
        pix = page.get_pixmap(matrix=fitz.Matrix(dpi / 72, dpi / 72))

        img = Image.frombytes("RGB", [pix.width, pix.height], pix.samples)

        # Redimensionner l'image à 50% de sa taille originale
        new_width = img.width // 2
        new_height = img.height // 2
        img = img.resize((new_width, new_height), Resampling.LANCZOS)

        img.save(f"extrait/ads-{counter}.webp", "WEBP")

        counter += 1

    pdf_document.close()
