# Convert PDF Pages To JPEG With Python

**URL:** <https://onehack.st/t/convert-pdf-pages-to-jpeg-with-python/56235>\
**Category:** Tutorials & Methods\
**Tags:** ebook, tips-tricks\
**Created:** [September 8, 2019, 9:50pm UTC](https://onehack.st/t/convert-pdf-pages-to-jpeg-with-python/56235 "2019-09-08T21:50:32Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![TheJoker](https://onehack.st/user_avatar/onehack.st/thejoker/32/22660_2.png) [@TheJoker](https://onehack.st/u/TheJoker)\
**Post date:** [September 8, 2019, 9:50pm UTC](https://onehack.st/t/convert-pdf-pages-to-jpeg-with-python/56235/1 "2019-09-08T21:50:32Z")

</div>

# CONVERT PDF PAGES TO JPEG WITH PYTHON

> A simple guide to extract images (jpeg, png) from PDF.

 ![282420a90962d46e9da0968782bc76c0](https://onehack.st/uploads/default/original/3X/0/f/0ff811804eab042bbc9b1447bf1ff8ed88a86d60.svg)

## Objectives:

- Extract Images from PDF

## Required Tools:

- [Poppler for windows](https://blog.alivate.com.au/poppler-windows/)— Poppler is a PDF rendering library . Include the pdftoppm utility
- [Poppler for Mac](http://macappstore.org/poppler/) — If HomeBrew already installed, can use brew install Poppler
- [Pdf2image](https://github.com/Belval/pdf2image)— Python module. Wraps the pdftoppm utility to convert PDF to a PIL Image object.

## Steps:

- 
  1. Install Poppler. For windows, Add “`xxx/bin/`” to env path

- 
  1. `pip install pdf2image`

**Usage**

```
import os
import tempfile
from pdf2image import convert_from_path
 
filename = 'target.pdf'
 
with tempfile.TemporaryDirectory() as path:
     images_from_path = convert_from_path(filename, output_folder=path, last_page=1, first_page =0)
 
base_filename = os.path.splitext(os.path.basename(filename))[0] + '.jpg'     
 
save_dir = 'your_saved_dir'
 
for page in images_from_path:
    page.save(os.path.join(save_dir, base_filename), 'JPEG')

```

> **Further notes**
> 
> - [https://stackoverflow.com/questions/46184239/python-extract-a-page-from-a-pdf-as-a-jpeg](https://stackoverflow.com/questions/46184239/python-extract-a-page-from-a-pdf-as-a-jpeg)
