我给出了下面的代码,它在linux平台上运行得很好,但是在winodws平台上无法执行。这和脓毒症的行为有关。在
def imgps():
try:
answer = filedialog.askdirectory(parent=root,
initialdir=os.getcwd(), title="Please select a folder:")
for fn in os.listdir(answer):
if fn.endswith(".png") or fn.endswith(".jpg"):
x = os.path.join(answer, fn)
print(x) #loop breaks after this.
rd = pytesseract.image_to_string(Image.open(x), lang='eng').replace('-\n', '') \
.replace('\n', ' ').encode("ascii", 'ignore')
tb9.insert('insert', rd)
tb9.insert('insert', br)
else:
continue
except:
messagebox.showinfo(message="No File to Display.")
有一个按钮点击执行以上功能。我想我不必把它的代码也给你。如有帮助,不胜感激。我所看到的print命令对第一个文件执行,然后循环中断,这证实了问题出在windows中的pytesseract行为。非常感谢您的帮助。在
最后,我不得不自己找到解决办法。有这样一个代码行。
pytesseract.pytesseract.tesseract_cmd = 'C:/Program Files/Tesseract-OCR/tesseract.exe'
它需要添加到函数中,以告诉pytesseract使用tesseract的位置。只有脚本和tesseract程序文件位于同一驱动器上时,设置环境变量才有效。如果他们在不同的驱动器,那么这是解决办法。在相关问题 更多 >
编程相关推荐