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.
It was easily realized using Python3.
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.
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