Python
PDF
JPEG
Image Conversion
Code Tutorial

Python - Extract a PDF page as a jpeg

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

Extracting a PDF page as a JPEG is a common task in document pipelines, previews, and thumbnail generation. In Python, the simplest path is usually pdf2image, which renders one or more pages and lets you save them with Pillow-compatible image methods.

pdf2image is a wrapper around Poppler tools, so it gives you solid rendering quality without making you shell out manually. Install the Python package first:

bash
pip install pdf2image pillow

You also need Poppler on the machine. On macOS that is commonly installed with Homebrew, and on Ubuntu with apt.

Once that dependency is present, extracting a single page is straightforward:

python
1from pdf2image import convert_from_path
2
3
4def extract_page_as_jpeg(pdf_path: str, page_number: int, output_path: str) -> None:
5    images = convert_from_path(
6        pdf_path,
7        first_page=page_number,
8        last_page=page_number,
9        dpi=200,
10        fmt="jpeg",
11    )
12
13    if not images:
14        raise ValueError(f"No page rendered for page {page_number}")
15
16    images[0].save(output_path, "JPEG", quality=90)
17
18
19extract_page_as_jpeg("report.pdf", 1, "page-1.jpg")

The page_number argument is one-based, which is easy to forget if you are used to zero-based indexing in Python collections.

Controlling Quality And Size

The dpi value controls how detailed the rendered image will be. Higher DPI gives better text sharpness, but it also increases rendering time and file size. For web previews, 150 to 200 DPI is often enough. For OCR or print-oriented export, 300 DPI may be a better baseline.

You can also resize the rendered image before saving:

python
1from pdf2image import convert_from_path
2
3
4images = convert_from_path("report.pdf", first_page=2, last_page=2, dpi=300)
5page = images[0]
6
7target_width = 1200
8ratio = target_width / page.width
9target_height = int(page.height * ratio)
10
11resized = page.resize((target_width, target_height))
12resized.save("page-2-preview.jpg", "JPEG", quality=85, optimize=True)

That pattern is useful when you want predictable preview dimensions instead of preserving the original render size.

Processing Multiple Pages Safely

Even if you only need one page most of the time, it helps to know how to generalize the code:

python
1from pathlib import Path
2from pdf2image import convert_from_path
3
4
5def export_pages(pdf_path: str, start_page: int, end_page: int, output_dir: str) -> None:
6    output = Path(output_dir)
7    output.mkdir(parents=True, exist_ok=True)
8
9    images = convert_from_path(
10        pdf_path,
11        first_page=start_page,
12        last_page=end_page,
13        dpi=200,
14    )
15
16    for index, image in enumerate(images, start=start_page):
17        image.save(output / f"page-{index}.jpg", "JPEG", quality=88)
18
19
20export_pages("report.pdf", 3, 5, "jpg-pages")

This keeps the conversion bounded to a specific range instead of loading an entire large document into memory.

Alternative With PyMuPDF

If you want fewer external system dependencies, PyMuPDF is another strong option:

python
1import fitz
2
3
4def extract_with_pymupdf(pdf_path: str, page_index: int, output_path: str) -> None:
5    doc = fitz.open(pdf_path)
6    page = doc.load_page(page_index)
7    pix = page.get_pixmap(dpi=200)
8    pix.save(output_path)
9    doc.close()
10
11
12extract_with_pymupdf("report.pdf", 0, "page-1.jpg")

This version uses zero-based page indexing, unlike the earlier pdf2image example. Both libraries work well, but you should standardize on one in a shared codebase to avoid confusion.

Common Pitfalls

The most common issue is missing Poppler. If pdf2image raises an error about pdfinfo or pdftoppm, the Python code is fine but the system dependency is not installed or not available on the PATH.

Another mistake is mixing page numbering conventions. pdf2image uses one-based page numbers in first_page and last_page, while some other libraries use zero-based indexes. Be explicit in helper function names and documentation.

Performance can also surprise you. Rendering an entire PDF at high DPI just to save one page wastes both memory and CPU. Limit the page range whenever possible.

Finally, JPEG is a lossy format. It is good for photos and previews, but for crisp screenshots of code or text-heavy pages, PNG may preserve edges better. Choose the image format based on the downstream use case instead of defaulting to JPEG every time.

Summary

  • 'pdf2image is a practical way to render PDF pages and save them as JPEG files.'
  • Install both the Python packages and the Poppler system tools.
  • Use first_page and last_page to render only the page range you need.
  • Tune dpi, quality, and optional resizing based on your output requirements.
  • Watch for page numbering differences and missing system dependencies.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.