【日常折腾】Python识别图片文字并对图片改名

1.前言

接到一个任务,将图片格式的专利的命名改为专利名称,效果如下。

在这里插入图片描述

2. 前期准备

安装openCV以及tesseract插件,tesseract插件需要设置为中文,推荐直接在PyCharm平台使用,借助pip直接安装。

3. 文字识别


# 头文件
import pytesseract
from PIL import Image

# 利用tesseract进行文字识别Image.open()括号里的是目标图片位置
image = Image.open('C:/Users/yangp/Desktop/2ext/新建文件夹/202207.jpg')
word = pytesseract.image_to_string(image, lang='chi_sim')
print(word)

上段代码可以直接对任意图片进行文字识别 读者可以自己试一下。

4. 文字过滤

大部分的文字被识别出来,我们可以直接copy图片中的专利名,用于更改图片名字,但这种改法,效率低,而且存在空格。
在这里插入图片描述

增加过滤函数remove。利用string.replace(“|”, “”),将括号中前一个字符如空格换成无。

def remove(string):
    string = string.replace(" ", "")
    string = string.replace("|", "")
    string = string.replace(":", "")
    string = string.replace(";", "")
    string = string.replace(".", "")
    string = string.replace("“", "")
    return string.replace("\n", "");

过滤后完整代码如下

import pytesseract
from PIL import Image


def remove(string):
    string = string.replace(" ", "")
    string = string.replace("|", "")
    string = string.replace(":", "")
    string = string.replace(";", "")
    string = string.replace(".", "")
    string = string.replace("“", "")
    return string.replace("\n", "");


image = Image.open('C:/Users/yangp/Desktop/2ext/新建文件夹/202207.jpg')
word = pytesseract.image_to_string(image, lang='chi_sim')
cword = remove(word)[10:500]

print(cword)

remove(code)[10:500]这里增加了一个字段范围的筛选,只要10~500之间,减少显示字数。

在这里插入图片描述

过滤后的文字删去多余字符,可以直接copy,这里有个错别字,看来这个文字识别效果一般。

6 图片改名

改名代码

newpath = 'C:/Users/yangp/Desktop/2ext/新建文件夹/'  # 保存图片位置
newname = "一种低压铸造电机外壳螺旋水道砂芯清理机及其操作方法"  # 0 是新,可以修改
out = '2' + '+' + newname + '.jpg'
print(out1)
image.save(newpath + out)
newpath:改名后保存地址
newname:改的名字:2+一种无风扇散热装置,可以增加序号。
image.save(newpath + out) 保存!

7.完整代码

import pytesseract
from PIL import Image


def remove(string):
    string = string.replace(" ", "")
    string = string.replace("|", "")
    string = string.replace(":", "")
    string = string.replace(";", "")
    string = string.replace(".", "")
    string = string.replace("“", "")
    return string.replace("\n", "");


image = Image.open('C:/Users/yangp/Desktop/2ext/新建文件夹/202207.jpg')
word = pytesseract.image_to_string(image, lang='chi_sim')
cword = remove(word)[10:500]

print(cword)

newpath = 'C:/Users/yangp/Desktop/2ext/新建文件夹/'  # 保存图片位置
newname = "一种无风扇散热装置"  #  可以将提取的文字人工筛选一下,放在这里,或者直接让cword=newname
out = '2' + '+' + newname + '.jpg'
print(out)
image.save(newpath + out)

8. 总结

其实最好效果是,识别出图片中想要的文字,然后自动给图片改名,搜索相关文献后,发现一种可以识别图片特定位置的文字,但是,对特定位置的识别中,识别图形效率高,识别文字差,且由于图片的变形,识别文字效果一般,受限于笔者技术,这里的代码算是半成品,抛砖引玉和大家交流。

  • 0
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值