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.
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.
Recommended Approach With pdf2image
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:
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:
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:
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:
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:
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
- '
pdf2imageis 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_pageandlast_pageto 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
- Python - Extracting and Saving Video Frames
- Python - Flask-SocketIO send message from thread not always working
- Python - Get path of root project structure
- Python - How can I make this code asynchronous?
- Python - How do I write a more efficient, Pythonic reduce?
- python - how to append numpy array to a pandas dataframe
- Python - How to intuit word from abbreviated text using NLP?
- Python - Is a dictionary slow to find frequency of each character?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack 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.