Python练习—画图软件生成的图像转成txt文件(只有0和1)

xiaoxiao2021-02-27  381

# Python3 # 图片文件在pictures文件夹 # 程序会生成pictures文件夹对应图片的二进制文件,保存在txt文件夹 import os from PIL import Image from os import listdir def picture2code(filename1,filename2): image_file = Image.open(filename1) # 缩放成60*70 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) # 黑白图像中0代表黑色,1代表白色 # 我希望有内容的部分表示为1,所以将0和1互换 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

转载请注明原文地址: https://www.6miu.com/read-2408.html

最新回复(0)