为什么此文件 I/O 代码在 Eclipse/PyDev 下不起作用但在终端中起作用?
Why does this file I/O code not work under Eclipse/ PyDev but works in terminal?
我有使用 tesseract 从图像文件中提取数据的工作代码,如下所示:
if src:
driver.get(src)
driver.save_screenshot('/Users/username/script/' + 'test.png')
image_name = 'test.png'
im = Image.open(image_name)
image_text = pytesseract.image_to_string(im)
print '\nImage Text:\t', image_text
当我执行代码时,此代码片段在 Mac 终端中没有任何错误,但是当我在 Eclipse 中使用 PyDev 执行相同操作时,它会抛出错误:
Exception: [Errno 2] No such file or directory
尝试执行行时:
im = Image.open(image_name)
为什么在 Eclipse 中会发生这种情况?
UPDATE:由于我的代码对少数人来说似乎很时髦,所以我已按以下方式对其进行了更改,但问题仍然存在(在 mac 终端上运行良好,但 Eclipse一直给我同样的错误)
if src:
driver.get(src)
image_name = 'test.png'
image_path = os.path.realpath(image_name)
driver.save_screenshot(image_path)
# read chart data from image
im = Image.open(image_path)
您的代码有点奇怪...您使用路径保存,然后使用 image_name。
尝试使用以下代码:
import os
filename = '/path/to/the/image.png'
driver.save_screenshot(filename)
if not os.path.exists(filename):
raise AssertionError("Image: %s does not exist" % (filename,))
Image.open(filename)
如果它不起作用,请粘贴堆栈跟踪的完整内容。
所以最终按照@Fabio 的建议使用回溯解决了问题。它与当前目录中不存在的图像文件无关,而是与在路径中找不到 tesseract 的问题有关。
在 pytesseract.py 文件中,它显示为:
# CHANGE THIS IF TESSERACT IS NOT IN YOUR PATH, OR IS NAMED DIFFERENTLY
tesseract_cmd = 'tesseract'
请注意,直接更改 pytesseract.py 文件不是一个好主意,但在导入 pytesseract 的任何文件中,添加以下行(tesseract 的路径将取决于您的特定 machine. ..在 mac 中,我能够使用 which
命令找到路径:which tesseract
)
pytesseract.pytesseract.tesseract_cmd = '/usr/local/bin/tesseract'
我有使用 tesseract 从图像文件中提取数据的工作代码,如下所示:
if src:
driver.get(src)
driver.save_screenshot('/Users/username/script/' + 'test.png')
image_name = 'test.png'
im = Image.open(image_name)
image_text = pytesseract.image_to_string(im)
print '\nImage Text:\t', image_text
当我执行代码时,此代码片段在 Mac 终端中没有任何错误,但是当我在 Eclipse 中使用 PyDev 执行相同操作时,它会抛出错误:
Exception: [Errno 2] No such file or directory
尝试执行行时:
im = Image.open(image_name)
为什么在 Eclipse 中会发生这种情况?
UPDATE:由于我的代码对少数人来说似乎很时髦,所以我已按以下方式对其进行了更改,但问题仍然存在(在 mac 终端上运行良好,但 Eclipse一直给我同样的错误)
if src:
driver.get(src)
image_name = 'test.png'
image_path = os.path.realpath(image_name)
driver.save_screenshot(image_path)
# read chart data from image
im = Image.open(image_path)
您的代码有点奇怪...您使用路径保存,然后使用 image_name。
尝试使用以下代码:
import os
filename = '/path/to/the/image.png'
driver.save_screenshot(filename)
if not os.path.exists(filename):
raise AssertionError("Image: %s does not exist" % (filename,))
Image.open(filename)
如果它不起作用,请粘贴堆栈跟踪的完整内容。
所以最终按照@Fabio 的建议使用回溯解决了问题。它与当前目录中不存在的图像文件无关,而是与在路径中找不到 tesseract 的问题有关。
在 pytesseract.py 文件中,它显示为:
# CHANGE THIS IF TESSERACT IS NOT IN YOUR PATH, OR IS NAMED DIFFERENTLY
tesseract_cmd = 'tesseract'
请注意,直接更改 pytesseract.py 文件不是一个好主意,但在导入 pytesseract 的任何文件中,添加以下行(tesseract 的路径将取决于您的特定 machine. ..在 mac 中,我能够使用 which
命令找到路径:which tesseract
)
pytesseract.pytesseract.tesseract_cmd = '/usr/local/bin/tesseract'