Looking for a PDF to image Python tutorial? You can convert PDF pages into JPG, JPEG, or PNG images using Python libraries such as PyMuPDF, pdf2image, and pypdfium2, or by calling command-line tools such as Poppler and Ghostscript from Python.
Python PDF-to-image conversion is useful for developers building document-processing applications, creating PDF thumbnails, preparing datasets, extracting page previews, or automating large document workflows.
For example, you might need to convert every page of a 20-page PDF into separate JPG images, generate thumbnails for uploaded documents, or render PDF pages for an OCR pipeline.
In this guide, you'll learn five practical methods to convert PDF to JPG using Python, including installation instructions, working code examples, batch processing, image-quality settings, and troubleshooting.
If you prefer converting documents without writing code, try the OneFileKit PDF to JPG converter.
Quick Answer: How to Convert PDF to Image Using Python
One straightforward method uses the PyMuPDF library.
First, install the package:
python -m pip install pymupdfThen create a Python file named convert_pdf.py:
import pymupdf
doc = pymupdf.open("input.pdf")
for page_number, page in enumerate(doc, start=1):
pix = page.get_pixmap(
dpi=200,
colorspace=pymupdf.csRGB,
alpha=False
)
pix.pil_save(
f"page-{page_number}.jpg",
format="JPEG",
quality=90
)
doc.close()Run the script:
python convert_pdf.pyThe script converts each PDF page into a separate JPG image.
For a five-page document, the output will contain:
page-1.jpgpage-2.jpgpage-3.jpgpage-4.jpgpage-5.jpg
PyMuPDF documents page rendering and image output in its official documentation.
Why Convert PDF to Image Using Python?
Python provides flexibility for processing documents programmatically.
Unlike manually converting PDFs through a graphical application, Python scripts can be integrated into automated workflows.
1. Automate Document Conversion
Convert uploaded PDF documents into JPG images automatically.
2. Generate PDF Thumbnails
Create image previews for document-management applications.
3. Prepare Documents for OCR
Render scanned PDF pages into images for text-recognition workflows.
4. Build Document Processing APIs
Integrate PDF-to-image conversion into web applications and backend services.
5. Process Multiple PDF Files
Convert hundreds of documents using batch scripts, subject to available system resources.
6. Control Image Resolution
Choose suitable DPI and JPEG quality settings for your application.
7. Support Multiple Image Formats
Export pages as JPG, PNG, or other supported image formats.
For occasional conversions, the OneFileKit PDF to JPG tool offers a browser-based alternative.
Method 1: Convert PDF to JPG Using PyMuPDF
PyMuPDF is a Python library for working with PDF documents and other supported document formats.
It can render PDF pages into raster images at a specified resolution.
Step 1: Install PyMuPDF
Open Terminal or PowerShell:
python -m pip install pymupdfStep 2: Create a Python Script
Create pdf_to_jpg.py.
Step 3: Add the Conversion Code
from pathlib import Path
import pymupdf
output_dir = Path("output")
output_dir.mkdir(exist_ok=True)
with pymupdf.open("input.pdf") as doc:
for i, page in enumerate(doc, start=1):
pix = page.get_pixmap(
dpi=200,
colorspace=pymupdf.csRGB,
alpha=False
)
pix.pil_save(
str(output_dir / f"page-{i}.jpg"),
format="JPEG",
quality=90
)
print("Conversion completed")Step 4: Run the Script
python pdf_to_jpg.pyStep 5: Check the Output
Open the output folder.
Each PDF page should have a corresponding JPG image.
How to Increase Resolution
Change:
dpi=200To:
dpi=300Higher resolution can improve the readability of small text but increases memory use and image dimensions.
Advantages of PyMuPDF
Direct PDF page rendering.
Configurable resolution.
Suitable for automated workflows.
Supports multiple image formats.
Provides extensive document-processing features.
Limitations
Large pages rendered at high DPI can consume substantial memory.
Some PDF features may render differently from the original viewer.
Check PyMuPDF's license terms before integrating it into commercial software.
For more information, see the official PyMuPDF image recipes.
Method 2: Convert PDF to JPG Using pdf2image
The pdf2image library provides a Python interface for converting PDF pages into images using Poppler utilities.
It is useful when you want a simple conversion API.
Step 1: Install pdf2image
python -m pip install pdf2imageStep 2: Install Poppler
The pdf2image library requires Poppler utilities.
On Ubuntu or Debian:
sudo apt install poppler-utilsOn macOS with Homebrew:
brew install popplerOn Windows, install a trusted compatible Poppler distribution and configure its executable path.
Step 3: Create a Script
from pathlib import Path
from pdf2image import convert_from_path
output_dir = Path("images")
output_dir.mkdir(exist_ok=True)
pages = convert_from_path(
"input.pdf",
dpi=200
)
for i, image in enumerate(pages, start=1):
image.convert("RGB").save(
output_dir / f"page-{i}.jpg",
"JPEG",
quality=90
)
print("PDF converted successfully")Step 4: Run the Script
python convert_pdf.pyStep 5: Review the JPG Images
Open the images folder.
More Memory-Efficient Conversion
For large PDFs, you can use direct JPEG output and an output directory:
from pdf2image import convert_from_path
paths = convert_from_path(
"input.pdf",
dpi=200,
fmt="jpeg",
output_folder="images",
paths_only=True
)
print(paths)Create the images folder before running this example.
Advantages of pdf2image
Simple Python interface.
Supports multiple pages.
Configurable DPI.
Integrates with Poppler.
Useful for common PDF rendering workflows.
Limitations
Requires Poppler.
Large documents can use significant memory.
Windows setup may require additional configuration.
Password-protected PDFs require appropriate authentication.
For installation instructions and options, consult the pdf2image documentation.
Method 3: Convert PDF to JPG Using pypdfium2
pypdfium2 provides Python bindings for PDFium, a PDF rendering engine.
It supports rendering PDF pages and converting the resulting images into formats such as JPG and PNG.
Step 1: Install pypdfium2
python -m pip install pypdfium2Step 2: Create the Conversion Script
from pathlib import Path
import pypdfium2 as pdfium
output_dir = Path("jpg_output")
output_dir.mkdir(exist_ok=True)
pdf = pdfium.PdfDocument("input.pdf")
try:
for i in range(len(pdf)):
page = pdf[i]
bitmap = page.render(
scale=200 / 72
)
image = bitmap.to_pil().convert("RGB")
image.save(
output_dir / f"page-{i + 1}.jpg",
"JPEG",
quality=90
)
page.close()
finally:
pdf.close()
print("All pages converted")Step 3: Run the Script
python convert_pdf.pyStep 4: Check the Output
The converted JPG images appear in the jpg_output folder.
What Does the Scale Value Mean?
PDF coordinates commonly use 72 points per inch.
For an approximate 200 DPI rendering:
scale=200 / 72For 300 DPI:
scale=300 / 72Advantages of pypdfium2
Uses the PDFium rendering engine.
Supports configurable rendering scale.
Suitable for application integration.
Supports conversion to Pillow images.
Can be used for document previews and thumbnails.
Limitations
Memory use increases with page dimensions and rendering scale.
Rendering options and package support may vary by version and operating system.
See the official pypdfium2 documentation.
Method 4: Convert PDF to JPG Using Python and Poppler
Python can execute Poppler's pdftoppm utility using the standard subprocess module.
This is useful when you already have Poppler installed and want to automate command-line conversion.
Step 1: Install Poppler
Install the appropriate Poppler utilities for your operating system.
Step 2: Create a Python Script
import subprocess
from pathlib import Path
output_dir = Path("output")
output_dir.mkdir(exist_ok=True)
subprocess.run(
[
"pdftoppm",
"-jpeg",
"-r", "200",
"input.pdf",
str(output_dir / "page")
],
check=True
)
print("Conversion completed")Step 3: Run the Script
python convert_pdf.pyStep 4: Review the Output
Poppler creates JPG files using the specified output prefix.
Convert at 300 DPI
Change:
"-r", "200"To:
"-r", "300"Convert a Single Page
import subprocess
subprocess.run(
[
"pdftoppm",
"-f", "3",
"-l", "3",
"-singlefile",
"-jpeg",
"-r", "200",
"input.pdf",
"page-3"
],
check=True
)This converts page three into page-3.jpg.
Advantages
Useful for automation.
Supports resolution settings.
Can convert selected pages.
Integrates with existing Poppler installations.
Avoids loading every page into a Python image list.
Limitations
Poppler must be installed separately.
The executable must be available in the system PATH or referenced using its full path.
For project information, visit the official Poppler website.
Method 5: Convert PDF to JPG Using Python and Ghostscript
Ghostscript is a document-processing interpreter that supports PDF rendering.
Python can call Ghostscript through subprocess to generate JPEG images.
Step 1: Install Ghostscript
On Ubuntu or Debian:
sudo apt install ghostscriptOn macOS:
brew install ghostscriptWindows users can install a compatible version from the official Ghostscript website.
Step 2: Create a Python Script
import subprocess
from pathlib import Path
output_dir = Path("ghostscript_output")
output_dir.mkdir(exist_ok=True)
subprocess.run(
[
"gs",
"-dSAFER",
"-dBATCH",
"-dNOPAUSE",
"-sDEVICE=jpeg",
"-r200",
"-dJPEGQ=90",
f"-sOutputFile={output_dir / 'page-%03d.jpg'}",
"input.pdf"
],
check=True
)
print("PDF converted to JPG")On Windows, the Ghostscript executable may be named gswin64c.exe instead of gs.
Step 3: Run the Script
python convert_pdf.pyStep 4: Review the Images
The output folder contains numbered JPG files.
Important Security Note
When processing untrusted PDFs, keep Ghostscript updated and use suitable process isolation and resource limits. The -dSAFER option alone should not be treated as a complete security boundary.
Advantages
Supports automated PDF rendering.
Configurable output resolution.
Supports JPEG quality settings.
Useful for command-line workflows.
Available on multiple operating systems.
Limitations
Ghostscript requires separate installation.
Rendering may not preserve every advanced PDF feature.
See the official Ghostscript documentation.
Five Python PDF to Image Methods Compared
Method | Rendering engine | Extra system installation | Best for |
|---|---|---|---|
PyMuPDF | MuPDF | Usually no | General Python PDF rendering |
pdf2image | Poppler | Yes | Simple conversion API |
pypdfium2 | PDFium | Usually no | PDF rendering integrations |
Python + pdftoppm | Poppler | Yes | Command-line automation |
Python + Ghostscript | Ghostscript | Yes | Custom rendering workflows |
Which Python Library Should You Choose?
For straightforward Python development, PyMuPDF is a practical starting point.
For a simple interface built around Poppler, pdf2image is convenient.
For applications that need PDFium rendering, pypdfium2 is worth considering.
For command-line automation, Poppler and Ghostscript offer additional flexibility.
Review each library's current license before using it in a commercial product.
How to Convert a Single PDF Page to JPG in Python
Sometimes you only need one page from a larger PDF.
For example, you may want to generate a thumbnail of the first page.
Using PyMuPDF:
import pymupdf
with pymupdf.open("input.pdf") as doc:
page = doc[0]
pix = page.get_pixmap(
dpi=150,
colorspace=pymupdf.csRGB,
alpha=False
)
pix.pil_save(
"thumbnail.jpg",
format="JPEG",
quality=85
)This converts the first page into a JPG image.
Python page indexing commonly starts at zero, so doc[0] refers to page one.
When Is Single-Page Conversion Useful?
Document thumbnails.
File previews.
Website upload previews.
Cover-page images.
Automated document indexing.
For users who prefer a graphical workflow, the OneFileKit PDF to JPG tool supports page-range selection.
How to Convert Multiple PDF Files to JPG Using Python
Batch conversion is useful when you need to process many PDF documents.
For example, you may have a folder containing:
invoice-01.pdfinvoice-02.pdfinvoice-03.pdfinvoice-04.pdf
You want to convert every page into JPG images.
Example Using PyMuPDF
from pathlib import Path
import pymupdf
input_dir = Path("pdf_files")
output_root = Path("converted_images")
output_root.mkdir(exist_ok=True)
for pdf_path in sorted(input_dir.glob("*.pdf")):
output_dir = output_root / pdf_path.stem
output_dir.mkdir(exist_ok=True)
with pymupdf.open(pdf_path) as doc:
for page_number, page in enumerate(doc, start=1):
pix = page.get_pixmap(
dpi=150,
colorspace=pymupdf.csRGB,
alpha=False
)
pix.pil_save(
str(output_dir / f"page-{page_number}.jpg"),
format="JPEG",
quality=85
)
print(f"Converted: {pdf_path.name}")How Does This Work?
The script:
Finds PDF files in the
pdf_filesfolder.Creates a separate output folder for each document.
Opens each PDF.
Renders its pages into JPG images.
Saves the converted files.
Continues with the next PDF.
Best Practices for Batch Conversion
Set maximum file sizes and page counts.
Process documents incrementally.
Handle corrupted PDFs gracefully.
Monitor memory and CPU usage.
Avoid overwriting existing files unintentionally.
Use safe filenames.
Clean up temporary files.
For large-scale systems, use a background job queue rather than processing every document within a single web request.
How to Convert PDF to JPG at 300 DPI Using Python
Higher-resolution rendering can be useful for detailed charts, small text, and print-oriented images.
PyMuPDF Example
import pymupdf
with pymupdf.open("input.pdf") as doc:
page = doc[0]
pix = page.get_pixmap(
dpi=300,
colorspace=pymupdf.csRGB,
alpha=False
)
pix.pil_save(
"page-300dpi.jpg",
format="JPEG",
quality=95
)What Does 300 DPI Mean?
A standard US Letter page measures approximately 8.5 × 11 inches.
At 300 DPI, the rendered image is approximately:
2,550 × 3,300 pixels.
This can provide more detail than a lower-resolution rendering.
However, it also increases memory use and file size.
For additional quality recommendations, read How to Convert PDF to JPG Without Losing Quality.
How to Convert PDF to PNG Using Python
JPG is not always the best output format.
For text-heavy pages, diagrams, and screenshots, PNG may preserve sharp edges better because it uses lossless image compression.
PyMuPDF PNG Example
import pymupdf
with pymupdf.open("input.pdf") as doc:
page = doc[0]
pix = page.get_pixmap(
dpi=200,
colorspace=pymupdf.csRGB,
alpha=False
)
pix.save("page-1.png")JPG vs PNG
Feature | JPG | PNG |
|---|---|---|
Compression | Lossy | Lossless |
Best for | Photographs | Text, diagrams, graphics |
Transparency | No standard alpha transparency | Supported |
File size | Often smaller for photographs | Depends on content |
Common use | Previews and web images | Screenshots and detailed graphics |
For browser-based PNG conversion, try the OneFileKit PDF to PNG tool.
How to Convert PDF to JPG Without Losing Quality in Python
JPEG is a lossy format, so completely lossless PDF-to-JPG conversion is not guaranteed.
However, you can minimize visible quality loss.
Use Appropriate DPI
For many document images, 150–300 DPI is a useful starting range.
Choose High JPEG Quality
Use an appropriate JPEG quality setting, such as 90–95, when supported.
Render From the Original PDF
Avoid repeatedly converting between PDF and JPG.
Preserve Aspect Ratio
Do not stretch the output image.
Check Small Text
Inspect footnotes, labels, and detailed graphics.
Consider PNG
For text-heavy documents or sharp line drawings, PNG may be preferable.
Keep Original Documents
Always retain the original PDF for future processing.
How to Generate PDF Thumbnails With Python
PDF thumbnails are useful for document-management systems, file upload previews, and content libraries.
You generally don't need to render a thumbnail at 300 DPI.
A lower resolution may be sufficient.
Example
import pymupdf
with pymupdf.open("input.pdf") as doc:
page = doc[0]
pix = page.get_pixmap(
dpi=72,
colorspace=pymupdf.csRGB,
alpha=False
)
pix.pil_save(
"thumbnail.jpg",
format="JPEG",
quality=80
)This creates a JPG preview of the first PDF page.
Thumbnail Best Practices
Use a consistent image width.
Preserve the page aspect ratio.
Avoid unnecessary high resolution.
Cache generated thumbnails.
Reuse thumbnails when the source PDF hasn't changed.
Provide descriptive filenames.
For a document-management website, generating thumbnails during file processing can improve the browsing experience.
How to Convert PDF to JPG in a Python Web Application
Developers can integrate PDF-to-image conversion into web applications.
A typical workflow looks like this:
A user uploads a PDF.
The backend validates the file.
The application stores it temporarily.
A worker processes the document.
Each page is rendered as an image.
JPG files are saved to an output directory or object storage.
The application provides a download link.
Temporary files are deleted according to the service's retention policy.
Important Backend Considerations
File Validation
Validate file types, sizes, and page counts.
Resource Limits
Set memory, CPU, and processing-time limits.
Security
Treat uploaded PDFs as untrusted files.
Background Processing
Use a job queue for large conversions.
Temporary Storage
Store documents in isolated temporary directories.
Error Handling
Return clear messages when processing fails.
Cleanup
Remove temporary files according to the configured retention policy.
For developers building a PDF conversion service, these safeguards are as important as the conversion code itself.
Can Python Convert Scanned PDFs to JPG?
Yes.
Scanned PDFs often contain raster images of printed pages.
Python PDF rendering libraries can convert those pages into JPG images.
Example Uses
Document previews.
Scanned page extraction.
OCR preparation.
Archiving workflows.
Visual document classification.
Does PDF to JPG Perform OCR?
No.
Rendering a PDF page into JPG creates an image.
OCR is a separate process that recognizes text within images.
If you need searchable text, use a compatible OCR workflow.
OneFileKit provides a PDF OCR tool for supported documents.
Common Python PDF to JPG Errors and Fixes
1. ModuleNotFoundError
Problem: Python cannot find the installed library.
Solution: Install the package in the same Python environment used to run your script.
2. PDFInfoNotInstalledError
Problem: pdf2image cannot locate Poppler utilities.
Solution: Install Poppler and configure its executable path.
3. FileNotFoundError
Problem: The PDF file does not exist at the specified location.
Solution: Check the filename and working directory.
4. PermissionError
Problem: Python cannot read the input file or write the output.
Solution: Check directory permissions.
5. Password-Protected PDF
Problem: The PDF requires authentication.
Solution: Use the library's supported password-handling options when authorized.
6. MemoryError
Problem: Rendering large pages consumes too much memory.
Solution: Reduce DPI, process pages individually, and limit concurrent conversions.
7. Blurry JPG Output
Problem: The output resolution or JPEG quality is too low.
Solution: Increase rendering resolution or use a higher JPEG quality setting.
8. Unsupported PDF Features
Problem: The rendering engine cannot process certain document elements correctly.
Solution: Try another compatible rendering library and inspect the output.
Is Python PDF to Image Conversion Safe?
Python PDF rendering can process documents locally, but local processing alone does not guarantee security.
PDF files can contain complex structures and potentially malformed content.
Security Recommendations
Keep rendering libraries updated.
Validate uploaded files.
Limit file sizes and page counts.
Restrict processing time and memory.
Use isolated worker processes.
Avoid executing untrusted shell commands.
Use argument lists rather than shell string interpolation.
Avoid exposing temporary files publicly.
Apply appropriate file permissions.
Delete temporary documents when no longer required.
When building a public document-conversion service, use defence-in-depth security controls.
Python PDF to Image vs Online Converter
Feature | Python libraries | OneFileKit |
|---|---|---|
Programming required | Yes | No |
Installation | Usually required | No |
Automation | Excellent | Browser workflow |
Batch processing | Scriptable | Subject to tool limits |
Custom DPI | Supported by several libraries | Quality presets |
Backend integration | Yes | Not required for ordinary users |
Best for | Developers | Everyday document conversion |
When Should You Use Python?
Use Python when you need custom automation, backend integration, or programmatic control.
When Should You Use OneFileKit?
Use OneFileKit PDF to JPG when you want to convert documents through a browser without writing code.
Related OneFileKit Articles and Tools
Explore these five relevant resources:
OneFileKit PDF to JPG — Convert PDF pages into JPG images without coding.
How to Change PDF to JPG: 5 Easy Methods — Explore general conversion methods.
How to Convert PDF to JPG Without Losing Quality — Learn about DPI and image quality.
OneFileKit PDF to PNG — Convert PDF pages into PNG images.
OneFileKit PDF OCR — Recognize text in supported scanned PDFs.
Final Thoughts
Learning PDF to image Python conversion gives developers several ways to automate document processing.
PyMuPDF, pdf2image, pypdfium2, Poppler, and Ghostscript provide different approaches for rendering PDF pages into JPG images.
For most developers, PyMuPDF or pypdfium2 is a useful starting point. pdf2image is convenient when Poppler is already installed, while command-line utilities offer additional automation options.
Choose a library based on rendering quality, performance, licensing, dependencies, and deployment requirements.
If you only need to convert a PDF occasionally, use the OneFileKit PDF to JPG converter.
It provides a browser-based workflow for converting PDF pages into JPG images without writing Python code.




