0

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')
martineau
  • 119,623
  • 25
  • 170
  • 301
CJD
  • 171
  • 1
  • 1
  • 13
  • @martineau I did see that but I understand it relates to how I want the PDF to be pixilated. When I change it between 300 and 600 it makes no difference on the JPG outputted. – CJD Dec 31 '21 at 20:58
  • Hmm, I see. Well as far as I can tell the only way to control the dpi of the image would be indirectly via the **`size`** argument. PDF's have pages of a specific size, say 8½ x 11 inches, if you wanted the get a 300 dpi image from it from it, you would need to specify `size=(2550, 3300)` because 2500 = 8.5 × 300 and 3300 = 11 × 300. Actually the docs say you only need to specify one, so `size=(2550, None)` ought to do the same thing. – martineau Dec 31 '21 at 21:22

0 Answers0