Skip to content

Instantly share code, notes, and snippets.

@akash-ch2812
Created July 1, 2020 06:44
Show Gist options
  • Save akash-ch2812/1e2c0991105d0ed2f0fa2cadbee00362 to your computer and use it in GitHub Desktop.
Save akash-ch2812/1e2c0991105d0ed2f0fa2cadbee00362 to your computer and use it in GitHub Desktop.
Code for converting PDF file to Image in Python
from pdf2image import convert_from_path
pdfs = r"provide path to pdf file"
pages = convert_from_path(pdfs, 350)
i = 1
for page in pages:
image_name = "Page_" + str(i) + ".jpg"
page.save(image_name, "JPEG")
i = i+1
@doughnet
Copy link

doughnet commented Feb 8, 2021

doesn't work.

output error:

Traceback (most recent call last):
  File "/root/miniconda3/lib/python3.8/site-packages/pdf2image/pdf2image.py", line 462, in pdfinfo_from_path
    raise ValueError
ValueError

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "PDF_to_Image.py", line 4, in <module>
    pages = convert_from_path(pdfs, 350)
  File "/root/miniconda3/lib/python3.8/site-packages/pdf2image/pdf2image.py", line 97, in convert_from_path
    page_count = pdfinfo_from_path(pdf_path, userpw, poppler_path=poppler_path)["Pages"]
  File "/root/miniconda3/lib/python3.8/site-packages/pdf2image/pdf2image.py", line 471, in pdfinfo_from_path
    raise PDFPageCountError(
pdf2image.exceptions.PDFPageCountError: Unable to get page count.
Syntax Warning: May not be a PDF file (continuing anyway)
Syntax Error: Couldn't find trailer dictionary
Syntax Error: Couldn't find trailer dictionary
Syntax Error: Couldn't read xref table
(base) root@OCR:~/text-extract# cat PDF_to_Image.py
from pdf2image import convert_from_path

pdfs = r"/root/text-extract/testFolder"
pages = convert_from_path(pdfs, 350)

i = 1
for page in pages:
    image_name = "Page_" + str(i) + ".jpg"
    page.save(image_name, "JPEG")
    i = i+1

@OmkarPatkar
Copy link

Add pdf file name at the end of your file path

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment