Rasterize PDF in Python

background

In certain environments, drawing vector PDFs sometimes did not work. To avoid this problem, I want to convert and save as a rasterized PDF such as JPEG instead of vector.

Method

It was easily realized using Python3.

Advance preparation

code

main.py


import pdf2image as p
from PIL import Image
import sys

#Rasterized resolution
#The larger the value, the more beautiful it is, but it takes time (the beauty is better at a certain value)
DPI = 300

#Image height of final output PDF
HEIGHT = 1200

pdfpath = sys.argv[1]
images = p.convert_from_path(pdfpath, dpi=DPI)

def resize(image):
    r = HEIGHT / image.height
    width = int(image.width * r)
    return image.resize((width, HEIGHT), Image.LANCZOS)

images = list(map(resize, images))
images = list(map(lambda image: image.convert('RGB'), images))

images[0].save('output.pdf',save_all=True, append_images=images[1:])

Rasterize to a higher resolution first, and then reduce to the image with the desired resolution to improve the image quality.

How to use

Suppose the PDF file you want to convert (hoge.pdf) is in the execution folder.

$ python main.py hoge.pdf

When the above command is executed, ʻoutput.pdf` will be generated in the executed directory.

Complete

Recommended Posts

Rasterize PDF in Python
OCR from PDF in Python
Convert markdown to PDF in Python
Quadtree in Python --2
Python in optimization
CURL in python
Metaprogramming in Python
Python 3.3 in Anaconda
Geocoding in python
SendKeys in Python
Meta-analysis in Python
Unittest in python
Epoch in Python
Discord in Python
Sudoku in Python
DCI in Python
quicksort in python
nCr in python
N-Gram in Python
Programming in python
Plink in Python
Constant in python
Lifegame in Python.
FizzBuzz in Python
Sqlite in python
StepAIC in Python
N-gram in python
LINE-Bot [0] in Python
Csv in python
Disassemble in Python
Reflection in Python
Constant in python
nCr in Python.
format in python
Scons in Python3
Puyo Puyo in python
python in virtualenv
PPAP in Python
Quad-tree in Python
Reflection in Python
Chemistry in Python
Hashable in python
DirectLiNGAM in Python
LiNGAM in Python
Flatten in python
flatten in python
Sorted list in Python
Daily AtCoder # 36 in Python
Clustering text in Python
Daily AtCoder # 2 in Python
Implement Enigma in python
Daily AtCoder # 32 in Python
Daily AtCoder # 6 in Python
Daily AtCoder # 18 in Python
Edit fonts in Python
Singleton pattern in Python
File operations in Python
Read DXF in python
Daily AtCoder # 53 in Python
Key input in Python
Use config.ini in Python