[英]pdf2image - convert_from_path returns an empty image for pdfs with colour
[英]FileNotFoundError while using the function convert_from_path() of the package pdf2image
我正在嘗試使用 Python 的庫pdf2image將我的 pdf 文件轉換為 png 文件。 我使用以下代碼來轉換我的 pdf 文件。
from pdf2image import convert_from_path, convert_from_bytes
pdf_file_path = './samples/my_pdf.pdf'
images = convert_from_path(pdf_file_path)
我想這樣做是為了以后使用pytesseract將我的 pdf 文件轉換為字符串文本。
即使文件在正確的路徑中,我不斷遇到的問題是以下 FileNotFound 錯誤。 誰能幫我弄清楚我做錯了什么?
---------------------------------------------------------------------------
FileNotFoundError Traceback (most recent call last)
<ipython-input-9-0b7f9e29e79a> in <module>()
1 from pdf2image import convert_from_path, convert_from_bytes
2 pdf_file_path = './samples/my_pdf.pdf'
----> 3 images = convert_from_path(pdf_file_path)
C:\Users\hamza.ameur\AppData\Local\Continuum\anaconda3\lib\site-packages\pdf2image\pdf2image.py in convert_from_path(pdf_path, dpi, output_folder, first_page, last_page, fmt)
22 uid, args, parse_buffer_func = __build_command(['pdftoppm', '-r', str(dpi), pdf_path], output_folder, first_page, last_page, fmt)
23
---> 24 proc = Popen(args, stdout=PIPE, stderr=PIPE)
25
26 data, err = proc.communicate()
C:\Users\hamza.ameur\AppData\Local\Continuum\anaconda3\lib\subprocess.py in __init__(self, args, bufsize, executable, stdin, stdout, stderr, preexec_fn, close_fds, shell, cwd, env, universal_newlines, startupinfo, creationflags, restore_signals, start_new_session, pass_fds, encoding, errors)
707 c2pread, c2pwrite,
708 errread, errwrite,
--> 709 restore_signals, start_new_session)
710 except:
711 # Cleanup if the child failed starting.
C:\Users\hamza.ameur\AppData\Local\Continuum\anaconda3\lib\subprocess.py in _execute_child(self, args, executable, preexec_fn, close_fds, pass_fds, cwd, env, startupinfo, creationflags, shell, p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite, unused_restore_signals, unused_start_new_session)
995 env,
996 os.fspath(cwd) if cwd is not None else None,
--> 997 startupinfo)
998 finally:
999 # Child is launched. Close the parent's copy of those pipe
FileNotFoundError: [WinError 2] The system cannot find the file specified
抱歉回復晚了。
挖掘到的源代碼之后pdf2image
,所述錯誤是由引起pdfinfo
,這是Unix和Linux基本命令,內側pdf2image
包。 因此,當您在缺少pdfinfo
命令的 windows 上使用此包時,會導致上述錯誤。
來自pdf2image
代碼:
#inside __page_count() function
...
else:
proc = Popen(["pdfinfo", pdf_path], stdout=PIPE, stderr=PIPE)
...
從上面的代碼可以看出,它調用了pdfinfo
一個子pdfinfo
來獲取pdf文件的頁數。
從以下網址下載窗口版 poppler 工具: http : //blog.alivate.com.au/poppler-windows/
解壓縮並將 bin 的位置(如 C:\\somepath\\poppler-0.67.0_x86\\poppler-0.67.0\\bin)添加到您的環境路徑中。
如果您正在打開,請重新啟動您的 CMD 和 python virtualenv
嘗試使用完整路徑。
例如:
import os
basePath = os.path.dirname(os.path.realpath(__file__))
pdf_file_path = os.path.join(basePath, "samples/my_pdf.pdf")
images = convert_from_path(pdf_file_path)
如果您使用 Google colab
首先使用以下命令運行單元格:
!apt-get install poppler-utils
這是一個完整的示例筆記本,安裝 deps,下載示例 PDF,然后使用 pdf2image 將其轉換為圖像以進行顯示。
https://colab.research.google.com/drive/10doc9xwhFDpDGNferehBzkQ6M0Un-tYq
我在運行 Python 2 時遇到了這個問題。
再次查看后,pypi 頁面明確指出該代碼與 Python 2 不兼容。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.