gowthami
gowthami

Reputation: 239

How to convert the pdf file to jpeg images

Here is my program, I want to convert pdf file into jpeg images, I wrote below code I am getting the PIL.PpmImagePlugin object how can I convert to jpeg format can you please help me. Thank you in advance.

from pdf2image import convert_from_path
images = convert_from_path('/home/cioc/Desktop/testingFiles/pdfurl-guide.pdf')
print images

Upvotes: 0

Views: 1505

Answers (2)

Diptangsu Goswami
Diptangsu Goswami

Reputation: 5975

You can add an output path and an output format for the images. Each page of your pdf will be saved in that directory in the specified format.

Add these keyword arguments to your code.

images = convert_from_path(
    '/home/cioc/Desktop/testingFiles/pdfurl-guide.pdf',
    output_folder='img',
    fmt='jpeg'
)

This will create a directory named img and save each page of your pdf as a jpeg image inside img/

Alternatively, you can save each page using a loop by calling save() on each image.

from pdf2image import convert_from_path


images = convert_from_path('/home/cioc/Desktop/testingFiles/pdfurl-guide.pdf')

for page_no, image in enumerate(images):
    image.save(f'page-{page_no}.jpeg')

Upvotes: 1

Belval
Belval

Reputation: 1516

You could use pdf2image parameter fmt='jpeg' to make it return JPEG instead.

You can also just manipulate the PPM as a you would a normal JPEG as this is only the backend file type. If you do Image.save('path.jpg') it will save it as a JPEG.

Upvotes: 1

Related Questions