本帖最后由 xiaoxue 于 2019-3-4 20:49 编辑
简单点的思路 。
图像转换jpg -> post 到 baiduAi -> 返回识别的文字(带坐标)-> 根据返回的坐标Image.new() 生成个#FFfff背景的图像->利用Image.paste() 把#fff图像覆盖住水印- # -*- coding: utf-8 -*-
- from aip import AipOcr
- import time
- import random
- import sys,re
- import requests,json
- from PIL import Image, ImageDraw,ImageFont
- import base64
- # """ 你的 APPID AK SK """
- APP_ID = xxx'
- API_KEY = 'xxxx'
- SECRET_KEY = 'xxxxx'
- client = AipOcr(APP_ID, API_KEY, SECRET_KEY)
- a = client.accurate(image)
- def get_file_content(filePath):
- with open(filePath, 'rb') as fp:
- return fp.read()
- def Wordfilter(words, arr):
- for str in arr:
- if words.find(str) >=0:
- return True
- return False
- for juzi in a['data']['words_result']:
- filterKey = [‘股票学习网','股票','股票学习','股票学','习网','www.']
- if Wordfilter(juzi['words'],filterKey):
- cropZB = juzi['location']
- left = cropZB['left']
- top = cropZB['top']
- right = left + cropZB['width']
- bottom = top + cropZB['height']
- TC_img = Image.new("RGB",(cropZB['width'],cropZB['height']),"#FFFFFF")
- im.paste(TC_img,(left, top, right, bottom))
-
- im.save(file)
复制代码
处理前:
处理后:
一些小技巧:
im.resize((w*2, h*2),Image.ANTIALIAS)
im.resize((w*3, h*3),Image.ANTIALIAS)
im.resize((w*4, h*4),Image.ANTIALIAS)
通过修改 宽和高: 来提高 文字识别的准确率度。
处理完后im.resize((w, h),Image.ANTIALIAS).save('xx.jpg')
|