【发布时间】:2021-01-06 07:02:59
【问题描述】:
这是我尝试执行的代码,用于从图像中提取文本并保存在路径中。
def main():
path =r"D drive where images are stored"
fullTempPath =r"D drive where extracted texts are stored in xls file"
for imageName in os.listdir(path):
inputPath = os.path.join(path, imageName)
img = Image.open(inputPath)
text = pytesseract.image_to_string(img, lang ="eng")
file1 = open(fullTempPath, "a+")
file1.write(imageName+"\n")
file1.write(text+"\n")
file1.close()
file2 = open(fullTempPath, 'r')
file2.close()
if __name__ == '__main__':
main()
我收到以下错误,有人可以帮我解决这个问题
UnicodeEncodeError Traceback (most recent call last)
<ipython-input-7-fb69795bce29> in <module>
13 file2.close()
14 if __name__ == '__main__':
---> 15 main()
<ipython-input-7-fb69795bce29> in main()
8 file1 = open(fullTempPath, "a+")
9 file1.write(imageName+"\n")
---> 10 file1.write(text+"\n")
11 file1.close()
12 file2 = open(fullTempPath, 'r')
~\anaconda3\lib\encodings\cp1252.py in encode(self, input, final)
17 class IncrementalEncoder(codecs.IncrementalEncoder):
18 def encode(self, input, final=False):
---> 19 return codecs.charmap_encode(input,self.errors,encoding_table)[0]
20
21 class IncrementalDecoder(codecs.IncrementalDecoder):
UnicodeEncodeError: 'charmap' codec can't encode character '\ufb01' in position 2090: character maps to <undefined>
【问题讨论】:
-
我还没有在这个网站上找到一种方法来禁用 pytesseract 中的连字输出,但stb-tester.com/blog/2014/04/14/improving-ocr-accuracy 可能有一些东西。
-
还有python - Convert hexadecimal character (ligature) to utf-8 character - Stack Overflow,用来修补tesseract的输出;虽然这可能不太准确?