что-то вроде этого работает для меня. необходимо использовать массив numpy дляimg2
def look_at_img(self, filename, x = 0, y = 0, width = 28, height = 28):
img = Image.open(filename)
img2 = [[0] * 28] * 28
img2 = np.asarray(img2, dtype="float32") ## 'img2' MUST BE A NUMPY ARRAY!!
oneimg = []
mnist_dim = 28
multx = width / float(mnist_dim)
multy = height / float(mnist_dim)
xy_list = []
dimx, dimy = img.size
counter = 0
''' Put in shrunk form. '''
if not len (img.getbands()) < 3 :
if not (x + width > dimx and y + height > dimy) :
for aa in range(28) :
for bb in range(28) :
astart = x + aa * multx
bstart = y + bb * multy
if astart >= 0 and astart < dimx and bstart >= 0 and bstart < dimy :
item = [ aa, bb, list(img.getpixel((int(astart) ,int(bstart))))]
xy_list.append(item)
counter = counter + 1
''' Put list in 28 x 28 array. '''
if len(xy_list) == 0:
xy_list = [[0, 0,[0,0,0]]]
''' just one color '''
high = img.getextrema()[0][1] /2
for i in range(len(xy_list)):
q = xy_list[i]
color = q[2][0]
if color > high : img2[int(q[0]), int(q[1])] = color
''' Then add entire array to oneimg variable and flatten.'''
for yz in range(28):
for xz in range(28):
oneimg.append(img2[yz][xz])
return oneimg