import os
from PIL
import Image
from os
import listdir
def picture2code(filename1,filename2):
image_file = Image.open(filename1)
image_file = image_file.resize((
60,
70))
image_file = image_file.convert(
'1')
width,height=image_file.size
f1=open(filename1,
'r')
f2=open(filename2,
'w')
for i
in range(height):
for j
in range(width):
pixel=int(image_file.getpixel((j,i))/
255)
if(pixel==
0):
pixel=
1
elif(pixel==
1):
pixel=
0
f2.write(str(pixel))
if(j==width-
1):
f2.write(
'\n')
f1.close()
f2.close()
path_picture=
'pictures'
path_txt=
'txt'
pictureList=listdir(path_picture)
m=len(pictureList)
for i
in range(m):
pictureNameStr=pictureList[i]
picturelocation=os.path.join(path_picture, pictureNameStr)
pictureStr=pictureNameStr.split(
'.')[
0]
txtlocation=os.path.join(path_txt,
'%s.txt'%pictureStr)
picture2code(picturelocation,txtlocation)
该程序可以用于手写识别,利用一系列手写图像生成二进制文本。
程序picture和源码: 链接:http://pan.baidu.com/s/1dF3SGyl 密码:4z3q