Hiro
Hiro

Reputation: 51

I want to convert pdf to image using pdf2image in python on Mac OS X

I want to convert pdf to image using pdf2image in python on Mac OS X.

    from pdf2image import convert_from_path, convert_from_bytes
from pdf2image.exceptions import (
    PDFInfoNotInstalledError,
    PDFPageCountError,
    PDFSyntaxError
)
# define pdf path
# convert pdf to image(1200dpi)
pdf_path = Path(".")
images = convert_from_path(str(pdf_path), 1200)
# save image files one by one
image_dir = Path(".")
for i, page in enumerate(pages):
    file_name = pdf_path.stem + "_{:02d}".format(i + 1) + ".jpeg"
    image_path = image_dir / file_name
    # save JPEG
    page.save(str(image_path), "JPEG")

and then I get empty files... I cannot understand what is happening. Any thoughts from anyone??

Upvotes: 2

Views: 2743

Answers (1)

Dipen Shah
Dipen Shah

Reputation: 2444

Hiro

By using the pdf2image library can be used convert pdf to image like this way,

from pdf2image import convert_from_path
pages = convert_from_path('pdf_file', 500) // where 500 is dpi

Saving pages in jpeg format

for page in pages:
    page.save('out.jpg', 'JPEG')

For converting the first page of the PDF and nothing else check this Example,

from pdf2image import convert_from_path
pages = convert_from_path('file.pdf', 500)
pages = convert_from_path('file.pdf', 500, single_file=True)
pages[0].save('file.jpg', 'JPEG')

Upvotes: 1

Related Questions