#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import glob
from os import path
import os
import pytesseract
from PIL import Image
from queue import Queue
import threading
import datetime
import cv2
def convertimg(picfile, outdir):
'''调整图片大小,对于过大的图片进行压缩
picfile: 图片路径
outdir: 图片输出路径
'''
img = Image.open(picfile)
width, height = img.size
while (width * height > 4000000): # 该数值压缩后的图片大约 两百多k
width = width // 2
height = height // 2
new_img = img.resize((width, height), Image.BILINEAR)
new_img.save(path.join(outdir, os.path.basename(picfile)))
def baiduOCR(ts_queue):
while not ts_queue.empty():
picfile = ts_queue.get()
filename = path.basename(picfile)
outfile = 'D:\Study\pythonProject\scrapy\IpProxy\port_zidian.txt'
img = cv2.imread(picfile, cv2.IMREAD_COLOR)
print("正在识别图片:\t" + filename)
message = pytesseract.image_to_string(img,lang = 'eng')
message = message.replace('', '')
message = message.replace('\n', '')
# message = client.basicAccurate(img) # 通用文字高精度识别,每天 800 次免费
#print("识别成功!"))
try:
filename1 = filename.split('.')[0]
filename1 = ''.join(filename1)
with open(outfile, 'a+') as fo:
fo.writelines('\'' + filename1 + '\'' + ':' + message + ',')
fo.writelines('\n')
# fo.writelines("+" * 60 + '\n')
# fo.writelines("识别图片:\t" + filename + "\n" * 2)
# fo.writelines("文本内容:\n")
# # 输出文本内容
# for text in message.get('words_result'):
# fo.writelines(text.get('words') + '\n')
# fo.writelines('\n' * 2)
os.remove(filename)
print("识别成功!")
except:
print('识别失败')
print("文本导出成功!")
print()
def duqu_tupian(dir):
ts_queue = Queue(10000)
outdir = dir
# if path.exists(outfile):
# os.remove(outfile)
if not path.exists(outdir):
os.mkdir(outdir)
print("压缩过大的图片...")
# 首先对过大的图片进行压缩,以提高识别速度,将压缩的图片保存与临时文件夹中
try:
for picfile in glob.glob(r"D:\Study\pythonProject\scrapy\IpProxy\tmp\*"):
convertimg(picfile, outdir)
print("图片识别...")
for picfile in glob.glob("tmp1/*"):
ts_queue.put(picfile)
#baiduOCR(picfile, outfile)
#os.remove(picfile)
print('图片文本提取结束!文本输出结果位于文件中。' )
#os.removedirs(outdir)
return ts_queue
except:
print('失败')
if __name__ == "__main__":
start = datetime.datetime.now().replace(microsecond=0)
t = 'tmp1'
s = duqu_tupian(t)
threads = []
try:
for i in range(100):
t = threading.Thread(target=baiduOCR, name='th-' + str(i), kwargs={'ts_queue': s})
threads.append(t)
for t in threads:
t.start()
for t in threads:
t.join()
end = datetime.datetime.now().replace(microsecond=0)
print('删除耗时:' + str(end - start))
except:
print('识别失败')
实测速度慢,但用了多线程明显提高了速度,但准确度稍低,同样高清图片,90百分识别率。还时不时出现乱码文字,乱空格,这里展现不了,自己实践吧,重点免费的,随便识别,通向100张图片,用时快6分钟了,速度慢了一倍,但是是免费的,挺不错的了。
以上就是python利用pytesseract 实现本地识别图片文字的详细内容,更多关于python 识别图片文字的资料请关注其它相关文章!
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
暂无“python利用pytesseract 实现本地识别图片文字”评论...
稳了!魔兽国服回归的3条重磅消息!官宣时间再确认!
昨天有一位朋友在大神群里分享,自己亚服账号被封号之后居然弹出了国服的封号信息对话框。
这里面让他访问的是一个国服的战网网址,com.cn和后面的zh都非常明白地表明这就是国服战网。
而他在复制这个网址并且进行登录之后,确实是网易的网址,也就是我们熟悉的停服之后国服发布的暴雪游戏产品运营到期开放退款的说明。这是一件比较奇怪的事情,因为以前都没有出现这样的情况,现在突然提示跳转到国服战网的网址,是不是说明了简体中文客户端已经开始进行更新了呢?
更新动态
2025年10月28日
2025年10月28日
- 小骆驼-《草原狼2(蓝光CD)》[原抓WAV+CUE]
- 群星《欢迎来到我身边 电影原声专辑》[320K/MP3][105.02MB]
- 群星《欢迎来到我身边 电影原声专辑》[FLAC/分轨][480.9MB]
- 雷婷《梦里蓝天HQⅡ》 2023头版限量编号低速原抓[WAV+CUE][463M]
- 群星《2024好听新歌42》AI调整音效【WAV分轨】
- 王思雨-《思念陪着鸿雁飞》WAV
- 王思雨《喜马拉雅HQ》头版限量编号[WAV+CUE]
- 李健《无时无刻》[WAV+CUE][590M]
- 陈奕迅《酝酿》[WAV分轨][502M]
- 卓依婷《化蝶》2CD[WAV+CUE][1.1G]
- 群星《吉他王(黑胶CD)》[WAV+CUE]
- 齐秦《穿乐(穿越)》[WAV+CUE]
- 发烧珍品《数位CD音响测试-动向效果(九)》【WAV+CUE】
- 邝美云《邝美云精装歌集》[DSF][1.6G]
- 吕方《爱一回伤一回》[WAV+CUE][454M]