importtesserocrfrom PIL importImage, ImageDrawimporttime#image = Image.open("img/4_1.png")#fh = open("img/1.txt", "w")#w, h = image.size#图片转文本,测试用#for i in range(h):#for j in range(w):#cl = image.getpixel((j, i))#clall = cl[0] + cl[1] + cl[2]## clall == 0即当前像素为黑色#if clall == 0:#fh.write("0")#else:#fh.write("1")#fh.write("\n")#fh.close()
#将图片转为黑白二色
defblack_white(image):
w, h=image.sizefor i inrange(h):for j inrange(w):
cl=image.getpixel((j, i))
clall= cl[0] + cl[1] + cl[2]#clall == 0即当前像素为黑色
if clall >= 155*3: # 根据具体的图片修改
image.putpixel((j, i), (255, 255, 255))else:
image.putpixel((j, i), (0, 0, 0))#二值数组
t2val ={}deftwoValue(image,G):for y in range(0,image.size[1]):for x inrange(0,image.size[0]):
g=image.getpixel((x,y))if g >G:
t2val[(x,y)]= 1
else:
t2val[(x,y)]=0#降噪#根据一个点A的RGB值,与周围的8个点的RBG值比较,设定一个值N(0
defclearNoise(image,N,Z):for i inrange(0,Z):
t2val[(0,0)]= 1t2val[(image.size[0]- 1,image.size[1] - 1)] = 1
for x in range(1,image.size[0] - 1):for y in range(1,image.size[1] - 1):
nearDots=0
L=t2val[(x,y)]if L == t2val[(x - 1,y - 1)]:
nearDots+= 1
if L == t2val[(x - 1,y)]:
nearDots+= 1
if L == t2val[(x- 1,y + 1)]:
nearDots+= 1
if L == t2val[(x,y - 1)]:
nearDots+= 1
if L == t2val[(x,y + 1)]:
nearDots+= 1
if L == t2val[(x + 1,y - 1)]:
nearDots+= 1
if L == t2val[(x + 1,y)]:
nearDots+= 1
if L == t2val[(x + 1,y + 1)]:
nearDots+= 1
if nearDots
t2val[(x,y)]= 1
defsaveImage(filename,size):
image= Image.new("1",size)
draw=ImageDraw.Draw(image)for x inrange(0,size[0]):for y in range(0,size[1]):
draw.point((x,y),t2val[(x,y)])
image.save(filename)defstart(img_path,save_img_path):
image=Image.open(img_path)
black_white(image)
image= image.convert("L")
twoValue(image,100)
clearNoise(image,4,1)
saveImage(save_img_path,image.size)print(tesserocr.file_to_text(save_img_path))
img_path= "img/4.png"save_img_path= "img/4_1.png"start(img_path, save_img_path)