Python实现批量图像文字识别并指定部分提取

当我们在工作中需要批量识别同一区域内的一些数字时候可以用Python实现

需要用到这些库 easyocr openvc os matplotlib 核心是前两个

opevc实现图象裁剪,easyocr实现文字识别

安装方法

pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple

pip install opencv-contrib-python

pip install opencv-python

pip install opencv-python-headless

pip install easyocr

# -*- coding: utf-8 -*-
import easyocr 
import os
import cv2
import pandas as  pd

遍历图片并裁剪

# im_path:图片路径
 
def clip_image(im_path): 
    i=0 
    filelist = os.listdir(im_path)
    for file in filelist:
        file_path=os.path.join(im_path,file)
        im=cv2.imread(file_path)
        #[h,w]根据自己图片中目标的位置修改
        im=im[19:38,256:353] 
        b=str(i)   #数字变为字符串方便后面命名               
        save_path = r'E:\pythonprograms\easyocr\img\img2' #裁剪后路径
        save_path_file = os.path.join(save_path,b+".jpg")           
        cv2.imwrite(save_path_file,im)            
        i=i+1

im_path = r'E:\pythonprograms\easyocr\img\img'  #裁剪前路径
clip_image(im_path)

批量文字识别、拼接合并、转为excel存储

render = easyocr.Reader(['ch_sim','en'])
filepath =r'E:\pythonprograms\easyocr\img\img2' #裁剪后路径
file = os.listdir(filepath)
spa =[]
for f in file :
    url =os.path.join(filepath,f)
    content = render.readtext(url,detail=0) #detail=0 表示去掉细节 
    s = ' '.join(content)
    spa.append(s)
b2=pd.DataFrame(spa )
b2.to_excel("results.xls")  
b2
04.818 ruicms
18.907 ruicms
22.556 ruicms
33.280 ruicms
43.189 rnicms
53.028 rnicm

 相关知识

读取图片有三种方式,分别是matplotlib,opencv,PIL,展示用matplotlib比较简单

定位图像想要提取的区域,可以通过画图软件查看

比如鼠标移动在上面可以看到定位像素详细。就可以对应像素位置提取图片了

image1=r'E:\pythonprograms\easyocr\img\img\R-0148_01692_Screenshot.png'
im=cv2.imread(image1)
im2=im[19:38,256:353] 

cv2读取图片后,可以获取图像属性,可以裁剪图像

https://blog.csdn.net/yukinoai/article/details/86423937

# 获取图像属性
shape = im.shape
print('图像的形状为: ', shape)  # 打印图像形状,包括行、列、通道
size = im.size
print('图像的像素数目为: ', size)  # 打印图像的像素数目
dtype = im.dtype
print('图像的数据类型为: ', dtype)  # 打印图像的数据类型

用matplotlib读取图片 

import matplotlib.image as mpimg#读取图片
import matplotlib.pyplot as plt #显示图片
%matplotlib inline

image = mpimg.imread(image1)
plt.title('展示部分')
plt.axis('off')# 不显示坐标轴
plt.imshow(im2)
plt.show()

import matplotlib.image as mpimg#读取图片
import matplotlib.pyplot as plt #显示图片
%matplotlib inline

image = mpimg.imread(image1)
plt.title('Read Image by Matplotlib')
plt.axis('off')# 不显示坐标轴
plt.imshow(image)
plt.show()

过程中遇到的问题记录

easyocr import 报错 

easyocr安装没问题 但  import 报错

py运行错误为:

OMP: Error #15: Initializing libiomp5md.dll, but found libiomp5md.dll already initialized.

OMP: Hint This means that multiple copies of the OpenMP runtime have been linked into the program. That is dangerous, since it can degrade performance or cause incorrect results. The best thing to do is to ensure that only a single OpenMP runtime is linked into the process, e.g. by avoiding static linking of the OpenMP runtime in any library. As an unsafe, unsupported, undocumented workaround you can set the environment variable KMP_DUPLICATE_LIB_OK=TRUE to allow the program to continue to execute, but that may cause crashes or silently produce incorrect results. For more information, please see Intel® Product Support

ipynb 同样会报错

方法:

删除了E:\Anaconda\anacanda3\Library\bin下的这个libiomp5md.dll文件。也可以修改后缀名。

然后就可以了。

cv2 展示图片报错

使用,cv2.imshow(" ", img)  一直报错,最终也没解决,用的matplotlib展示图片。

  • 2
    点赞
  • 48
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
实现Python批量读取图片文字并保留格式,可以使用OCR技术对图片进行文字识别,然后将识别出的文本保存为相应格式的文件。 以下是一个示例代码,实现批量读取指定文件夹下的所有图片并进行OCR文字识别,最后将识别结果保存为txt文件: ```python import pytesseract from PIL import Image import os # 设置tesseract的安装路径 pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe' # 指定识别图片所在的文件夹路径 img_folder = r'C:\Users\username\Documents\images' # 定义一个函数,用于对单张图片进行OCR文字识别,并将识别结果保存到txt文件中 def recognize_text(image_path): # 打开图片 image = Image.open(image_path) # 进行OCR识别 text = pytesseract.image_to_string(image, lang='eng') # 保存识别结果到txt文件中 with open('result.txt', 'a') as f: f.write(text) f.write('\n\n') # 遍历指定文件夹下的所有图片,进行OCR文字识别 for root, dirs, files in os.walk(img_folder): for file in files: if file.endswith('.jpg') or file.endswith('.png'): image_path = os.path.join(root, file) recognize_text(image_path) ``` 如果要提取Word里的所有图片批量转化格式,可以使用Python的docx库来实现。以下是一个示例代码,实现提取Word文档中的所有图片并将其转化为jpg格式的图片: ```python from docx import Document # 打开Word文档 doc = Document('example.docx') # 定义一个函数,用于将Word文档中的图片保存为jpg格式的图片 def save_image(image, filename): with open(filename, 'wb') as f: f.write(image) # 遍历文档中的所有图片,并将其转化为jpg格式的图片 for i, image in enumerate(doc.inline_shapes): if 'image' in image._element.xml: # 获取图片数据 image_data = image._inline.graphic.graphicData.pic.blipFill.blip.getparent().getnext().getchildren()[0].getchildren()[0] # 将图片数据保存为jpg格式的图片 save_image(image_data, f'image{i}.jpg') ``` 需要注意的是,由于Word文档中的图片可能采用了不同的格式,因此转化为jpg格式的图片可能会失去一些细节。如果需要保留完整的图片格式,可以考虑将图片保存为原格式,或者使用第三方库进行格式转化。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

细节处有神明

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值