37,738
社区成员
发帖
与我相关
我的任务
分享

# -*- coding: utf-8 -*-
import Image
import ImageFont
import ImageDraw
def replaceNumbers(im, identifiedRegions, replacingContents):
box = []
draw = ImageDraw.Draw(im)
for k,v in identifiedRegions.iteritems():
replaceNumber = replacingContents[k]
print k,v
for box in v:
print box
height = box[3] - box[1]
font = ImageFont.truetype('c:/windows/fonts/arial.ttf', height)
im.paste('white', box)
txt = replacingContents[k]
draw.text((box[0], box[1]), txt, font=font, fill='blue')
return im
if __name__ == '__main__':
regions = {'a':[(20,30,60,50),(90,130,100,130)],
'b':[(200,200,250,250)]}
contents = {'a':33, 'b':30}
im = Image.open("d:\\testpngs\\sample1.png")
newim = replaceNumbers(im, regions, contents)
newim.save("d:\\testpngs\\result5.png")
import Image
import ImageDraw
import subprocess
def findbbox(im, sel):
text = tesseract(im)
# tesseract输出是按单字符和其坐标,转化成所需字串范围
word = ''.join([i[0] for i in text])
start = 0
boxes = []
for s in sel:
pos = word.find(s, start)
if pos == -1: continue
lx, ly = [], []
for i in range(len(s)):
data = text[pos+i].split()
lx.append(int(data[1]))
ly.append(int(data[2]))
lx.append(int(data[3]))
ly.append(int(data[4]))
# pil坐标原点在左上角, tesseract在左下角
box = (min(lx), im.size[1]-max(ly),
max(lx), im.size[1]-min(ly))
boxes.append(box)
start = pos + len(s)
return boxes
def tesseract(im):
# 图像另存为tesseract基本支持的uncompressed tiff
# 本例自行处理成黑白能完全识别,其它按情况不一...
mono = im.convert('L').point(lambda i: i > 245 and 255)
mono.save('tmp.tif', 'TIFF')
# tesseract命令行参数:
cmd = 'tesseract tmp.tif tmp batch.nochop makebox'
p = subprocess.Popen(cmd)
p.wait()
with open('tmp.txt') as f:
return f.readlines()
if __name__ == '__main__':
im = Image.open('sample1.png')
im.load()
draw = ImageDraw.Draw(im)
# 选定图片里的字,必须按顺序从左至右,由上而下
sel = ['60', '11cm', '60', '60']
for box in findbbox(im, sel):
draw.rectangle(box, outline='red')
im.save('makebox.png')
im.show()
# -*- coding: utf-8 -*-
import Image
import ImageFilter
import ImageFont
import ImageDraw
def findbbox(im, clr, ix=36, iy=12, ec=16, eb=3):
"""
im: obj of image
clr: color of text
iy: increment when scanning in y-dir
ix: increment when scanning in x-dir
ec: tolerance of color
eb: tolerance of bbox
return a list of bbox
"""
#分层做mask,叠加后取文字范围
mask = None
for band, c in zip(im.split(), clr):
band = band.point(lambda i: i <= c+ec and i >= c-ec and 255)
if mask is None:
mask = band
else:
mask.paste(band, mask=mask)
mbox = mask.getbbox()
if mbox is None: return
#对文字范围先yscan取单行范围,再xscan取单词范围
boxes = []
for box in yscan(mask.crop(mbox), iy):
ox, oy = mbox[0], mbox[1]
ybox = (ox+box[0], oy+box[1], ox+box[2], oy+box[3])
for box in xscan(mask.crop(ybox), ix):
ox, oy = ybox[0], ybox[1]
xbox = (ox+box[0]-eb, oy+box[1]-eb,
ox+box[2]+eb, oy+box[3]+eb)
boxes.append(xbox)
#im.crop(xbox).show()
return boxes
def yscan(im, incr):
oy, prev = 0, None
for y in range(0, im.size[1]+incr*2, incr):
curr = im.crop((0, oy, im.size[0], y)).getbbox()
if curr and curr == prev:
yield curr[0], curr[1]+oy, curr[2], curr[3]+oy
curr = None
if curr is None:
oy = y
prev = curr
def xscan(im, incr):
ox, prev = 0, None
for x in range(0, im.size[0]+incr*2, incr):
curr = im.crop((ox, 0, x, im.size[1])).getbbox()
if curr and curr == prev:
yield curr[0]+ox, curr[1], curr[2]+ox, curr[3]
curr = None
if curr is None:
ox = x
prev = curr
if __name__ == '__main__':
files = ['sample1.png', 'sample2.png']
for fname in files:
im = Image.open(fname)
im = im.filter(ImageFilter.MedianFilter)
boxes = findbbox(im, (255, 255, 0))
boxes += findbbox(im, (128, 0, 128))
txt = ['50\xb0', '65\xb0', '65\xb0', '100cm']
clr = ['red', 'green', 'cyan', 'blue']
height = max([box[3]-box[1] for box in boxes])
font = ImageFont.truetype('c:/windows/fonts/msyhbd.ttf', height)
draw = ImageDraw.Draw(im)
for i, box in enumerate(boxes):
im.paste('white', box)
draw.text((box[0], box[1]), txt[i], font=font, fill=clr[i])
im.show()
