一、源代码
from PIL import Image
im = Image.open("F:/result/4.jpg")
# 先将图片转为固定宽高如55*55以符合一行txt文件
im = im.resize((55,55))
fh = open("F:/result/7.txt", "a")
width = im.size[0]
height = im.size[1]
# k = im.getpixel((1,9)) 获取RBG颜色对照数:RBG(255,255,255)白色;RBG(0,0,0)黑色
for i in range(0,width):
for j in range(0,height):
cl = im.getpixel((i,j))
clall = cl[0]+cl[1]+cl[2]
if(clall==0):
#黑色
fh.write("1")
else:
#不是黑色
fh.write("0")
fh.write("\n")
fh.close()
二、原图片
三、识别后的txt文件
00000000000000000100000000000000
00000000000000000000000000000000
00000000000000000001000000000000
00000000000000000111110000000000
00000000000000000111111000000000
0000000000