Skip to content

Instantly share code, notes, and snippets.

@RobinDavid
Created November 14, 2024 22:50
Show Gist options
  • Save RobinDavid/a5a153107daa63ca4155fe63eb9bfd7e to your computer and use it in GitHub Desktop.
Save RobinDavid/a5a153107daa63ca4155fe63eb9bfd7e to your computer and use it in GitHub Desktop.
Extract the first page as a PNG file.
import fitz # PyMuPDF
import sys
from pathlib import Path
def extract_png_from_pdf(pdf_file, output_path):
# Ouvrir le fichier PDF
pdf_document = fitz.open(pdf_file)
# Sélectionner la première page
first_page = pdf_document[0]
# Extraire l'image au format PNG
pix = first_page.get_pixmap()
pix.save(output_path)
# Fermer le fichier PDF
pdf_document.close()
if __name__ == "__main__":
if len(sys.argv) != 2:
print("Usage extract-pixmap.py FILE.PDF")
sys.exit(1)
pdf_file = Path(sys.argv[1])
out_file = pdf_file.with_suffix(".png")
extract_png_from_pdf(pdf_file, out_file)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment