最后,我找到了一个可以使用的解决方案.
使用pypdfocr及其pypdfocr_gs库我打电话
pypdfocr.pypdfocr_gs.PyGs({}).make_img_from_pdf(pdf_file)
检索jpg图像然后我使用PIL从它获取ImageTk.PhotoImage实例并在我的代码中使用它们.
ImageTk.PhotoImage(_img_file_handle)
我会尽快添加一个正确的例子.
编辑:
正如承诺的那样,代码
import pypdfocr.pypdfocr_gs as pdfImg
from PIL import Image, ImageTk
import Tkinter as tk
import ttk
import glob, os
root=tk.Tk()
__f_tmp=glob.glob(pdfImg.PyGs({}).make_img_from_pdf("\tmp\test.pdf")[1])[0]
# ^ this is needed for a "default"-Config
__img=Image.open(__f_tmp)
__tk_img=ImageTk.PhotoImage(__img)
ttk.Label(root, image=__tk_img).grid()
__img.close()
os.remove(__f_tmp)
root.mainloop()
编辑:
使用viranthas pypdfocr版本,在处理Windows 10和pythons子进程中似乎存在一个错误:
# extract from pypdfocr_gs:
def _run_gs(self, options, output_filename, pdf_filename):
try:
cmd = '%s -q -dNOPAUSE %s -sOutputFile="%s" "%s" -c quit' % (self.binary, options, output_filename, pdf_filename)
logging.info(cmd)
# Change this line for Windows 10:
# out = subprocess.check_output(cmd, shell=True)
out = subprocess.check_output(cmd)
# end of extract