Created
November 14, 2024 22:50
-
-
Save RobinDavid/a5a153107daa63ca4155fe63eb9bfd7e to your computer and use it in GitHub Desktop.
Extract the first page as a PNG file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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