将图像文本写入新的文本文件?

Write image text to a new text file?

我正在使用 tesseract 进行 OCR。我在 ubuntu 18.04.

我有这个程序可以从图像中提取文本并打印出来。我想让那个程序创建一个新的文本文件并将提取的内容粘贴到新的文本文件中,但我只能做这些

这是我从图像中提取文本的程序

from pytesseract import image_to_string 
from PIL import Image
print image_to_string(Image.open('sample.jpg'))

这是将文本复制到剪贴板的程序,

import os
def addToClipBoard(text):
    command = 'echo ' + text.strip() + '| clip'
    os.system(command)

此程序将打开 geditor 并创建一个新的文本文件

import subprocess
proc = subprocess.Popen(['gedit', 'file.txt'])

如有任何帮助,我们将不胜感激。

如果您只需要文本,请打开一个文本文件并写入:

from pytesseract import image_to_string 
from PIL import Image
text =  image_to_string(Image.open('sample.jpg'))

with open('file.txt', mode = 'w') as f:
    f.write(text)

正如我在评论中建议的那样,创建一个新文件并将提取的文本写入其中:

with open('file.txt', 'w') as outfile:
    outfile.write(image_to_string(Image.open('sample.jpg')))