用Python进行简单的图片数字识别(1)

一、源代码

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()


二、原图片

用Python进行简单的图片数字识别(1)_第1张图片

 

三、识别后的txt文件

00000000000000000100000000000000
00000000000000000000000000000000
00000000000000000001000000000000
00000000000000000111110000000000
00000000000000000111111000000000
00000000000000001010111100000000
00000000000000010000111110000000
00000000000000100000011110000000
00000000000001000000011111000000
00000000001011100000011111000100
00000000010110100000011111111110
00000000000011000000011111111110
00000000010010000000011111111110
00000001110100000000011111111110
00000001111000000000001111111100
00000011110000000000011111100000
00001011110000000001111111100000
00011111100000000111111111000000
00011111000000001110001111000000
00111111100001111100001111000000
01111111100011111100001111000000
01111111000111111100001111000000
01111110001111100000001111000000
00101110001111000000011111000000
00000000000100000000011111000000
00000000000000000000001111000000
00000000000000000000011111000000
00000000000000000000111111000000
00000000000000000000011111000000
00000000000000000000011110000000
00000000000000000000010110000000
00000000000000000000000000000000

你可能感兴趣的:(初学,数据分析)