I have a simple code to convert pdf to jpg. I need the jpg output to be 1200 by 1500 pixels and 4 by 5 inches. I need 300 dpi. When I run the code it produces a jpeg with 96 dpi (1200 by >1500 - but I wanted to keep the ratio). I checked the docs but was unable to find how to change dpi output. I took the file and cropped it using Windows native Paint program to get 1200 by 1500 pixels. When I saved it from Paint it was 120 dpi.
import os
from pdf2image import convert_from_path
rel_path = os.path.dirname(__file__)
my_pdfs = ['IN.pdf']
my_jpgs = ['OUT.jpg']
for in_file, out_file in zip(my_pdfs, my_jpgs):
filetoconvert = os.path.join(rel_path, in_file)
filetosave = os.path.join(rel_path, out_file)
page = convert_from_path(filetoconvert, dpi=600, fmt='jpeg',
jpegopt={
'quality':95,
'progressive':True,
'optimize':True},
size=(1200,None)
)
#print(type(page))
for pp in page:
pp.save(filetosave, 'JPEG')