[PYTHON] Convert PDF to image with ImageMagick

Settings for handling PDF in ImageMagick

When handling PDF with ImageMagick, GS must be installed.

http://superuser.com/questions/819277/cant-convert-pdf-into-image-because-of-no-images-defined-error

If you are using brew on OSX, you can use brew install gs.

Sampling resolution setting required

If you don't set the dpi with the density option, the image will be very grainy. Set according to the original PDF settings and the size of the image you want to output.

Referenced site http://icepotato.cocolog-nifty.com/blog/2013/06/imagemagickpdfw.html

PDF cannot be read by Pillow of Python

With Python Pillow, PDF seems to be writing only and not reading.

Code to convert whole folder PDF to PNG

The sampling resolution is 300dpi, the margins are trimmed, and the PNG image is exported to the same folder.

It should work if you have ImageMagick installed.

# coding: utf-8

import os
import os.path
import fnmatch
import subprocess


def execute(root_path):
    for dirpath, _, filenames in os.walk(root_path):
        for filename in filenames:
            if fnmatch.fnmatch(filename, u"*.pdf"):
                org_path = os.path.join(dirpath, filename)
                png_path = org_path.replace(".pdf", ".png ")

                print "convert {0} to {1}".format(org_path, png_path)

                if subprocess.call(["convert", "-density", "300", "-trim", org_path, png_path]) != 0:
                    print "failed: {0}".format(org_path)
        
if __name__ == '__main__':
    root_path = raw_input("target folder path> ")
    execute(root_path)

Recommended Posts

Convert PDF to image with ImageMagick
Convert PDF to image (JPEG / PNG) with Python
Convert the image in .zip to PDF with Python
Convert PDF files to PNG files with GIMP
Sample to convert image to Wavelet with Python
Convert 202003 to 2020-03 with pandas
I want to convert an image to WebP with lollipop
Convert .ipynb to .html (with BatchFile)
Convert PDF to Documents by OCR
Convert markdown to PDF in Python
Workflow to convert formula (image) to python
Batch convert image files uploaded to MS Forms / Google Forms to PDF
Convert sentences to vectors with gensim
Convert garbled scanned images to PDF with Pillow and PyPDF
Convert from pdf to txt 2 [pyocr]
Convert a string to an image
Convert files written in python etc. to pdf with syntax highlighting
Convert memo at once with Python 2to3
Image classification with Keras-From preprocessing to classification test-
Introduction to Python Image Inflating Image inflating with ImageDataGenerator
Convert character strings to features with RoBERTa
Convert Excel data to JSON with python
Convert Hiragana to Romaji with Python (Beta)
Convert FX 1-minute data to 5-minute data with Python
Convert PDF attached to email to text format
Convert array (struct) to json with golang
Convert HEIC files to PNG files with Python
Convert Chinese numerals to Arabic numerals with Python
Try to generate an image with aliasing
Library comparison summary to generate PDF with Python
Convert DICOM to PNG with Ascending and Descending
Crop the image to rounded corners with pythonista
Convert data with shape (number of data, 1) to (number of data,) with numpy.
Convert PDFs to images in bulk with Python
Convert mp4 to mp3 with ffmpeg (thumbnail embedded version)
Convert svg file to png / ico with Python
How to crop an image with Python + OpenCV
Convert Windows epoch values to date with python
Easily convert Jupyter Notebooks to blogs with fastpages
How to convert SVG to PDF and PNG [Python]
Convert multiple jpg files to one PDF file
Batch convert PSD files in directory to PDF
How to convert (32,32,3) to 4D tensor (1,32,32,1) with ndarray type
[Small story] Easy way to convert Jupyter to PDF
Post an article with an image to WordPress with Python
Convert strings to character-by-character list format with python
Convert Mobile Suica usage history PDF to pandas Data Frame format with tabula-py
How to convert Web pages to PDF, PNG, JPG with VBA (Excel) (Selenium Basic)
Images created with matplotlib shift from dvi to pdf
How to convert / restore a string with [] in python
Beginners try to convert Word files to PDF at once
How to scrape image data from flickr with python
Image recognition with keras
0 Convert unfilled date to datetime type with regular expression
Convert kanji to kana
Convert a text file with hexadecimal values to a binary file
How to convert horizontally held data to vertically held data with pandas
How to convert a class object to a dictionary with SQLAlchemy
Convert jupyter to py
[Python] Convert PDF text to CSV page by page (2/24 postscript)
Convert keras-yolo3 to onnx