numpages = [1,2,3,4,5,6,7,31,45,79,144,177]
docPDF = "Maths_expertes_terminale.pdf"

import fitz  # PyMuPDF
from PIL import Image
from PIL.Image import Resampling  # Importer Resampling directement

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/terminale-expertes-{counter}.webp", "WEBP")

        counter += 1

    pdf_document.close()