美文网首页
Python-140 Extract txt from Imag

Python-140 Extract txt from Imag

作者: RashidinAbdu | 来源:发表于2022-03-23 11:04 被阅读0次
    • 问题:刚好需要整理一些之前参加的会议啥的,但是发现当时只存了图片文件或者海报的截图,那么怎么办呢?其次英文和中文的字符识别还不太一样,然后提取后还需要合并到一个txt文件,以便后续整理。所以具体实现如下:

    1. 英文的提取:图片中的英文字,数字都基本可以准确提取,步骤为:
    • 查找后会发现,安装地址为:
      C:\Program Files\Tesseract-OCR\tesseract.exe

    • 然后先安装以下包:

    pip install pytesseract
    

    图片为:


    image.png
    • 然后按照以下代码运行:
    from PIL import Image
    import PIL.Image
    
    from pytesseract import image_to_string
    # Import modules
    from PIL import Image
    import pytesseract
    
    # Include tesseract executable in your path
    pytesseract.pytesseract.tesseract_cmd = r"C:\Program Files\Tesseract-OCR\tesseract.exe"
    
    # Create an image object of PIL library
    image = Image.open('C:\\Users\\Administrator\\Desktop\\捕获.PNG')
    
    # pass image into pytesseract module
    # pytesseract is trained in many languages
    image_to_text = pytesseract.image_to_string(image, lang='eng')
    
    # Print the text 
    print(image_to_text)
    
    # write the result in to a txt file 
    f = open (r'C:\\Users\\Administrator\\Desktop\\all.txt','w')
    
    print (image_to_text,file = f)
    
    f.close()
    

    运行结果为:


    image.png

    即:成功获取图片中的文字部分;

      1. 中文文字的提取:
        需要下载语言包,就是这个网站里下载中文简体的:下载完,直接放到安装位置的tessdata文件夹里就好了。

    Traineddata Files for Version 4.00 + | tessdoc (tesseract-ocr.github.io)

    image.png
    • 多个txt的合并:
    
    import os
    mergefiledir="D:\\GRAD_COURSES\\Ph.D_Publications\\2021_Publications\\KCTC-Deposition\\1"
    
    filenames=os.listdir(mergefiledir)
    file=open('D:\\GRAD_COURSES\\Ph.D_Publications\\2021_Publications\\KCTC-Deposition-Names.txt','w')
    
    for filename in filenames:
         filepath=mergefiledir+'\\'+filename
         for line in open(filepath): file.writelines(line)
         file.write('\n')
    file.close()
    

    相关文章

      网友评论

          本文标题:Python-140 Extract txt from Imag

          本文链接:https://www.haomeiwen.com/subject/hpdcjrtx.html