我正在研究一些打开jpg的python代码,然后转到图像的某个部分并提取该部分。代码意味着采用一定大小的矩形并将其压缩到28乘以28。现在我的代码总是得到下面的输出。输出显示重复28次的一行数据。我希望输出有所不同,但事实并非如此。我希望这是某人可以轻易发现的东西。任何帮助,将不胜感激。感谢。
def start_here(self):
... # x,y,w,h are all valid
filename = "some valid filename"
img = self.look_at_img(filename,x,y,w,h)
self.print_block(img)
def look_at_img(self, filename, x = 0, y = 0, width = 28, height = 28):
img = Image.open(open(filename))
size = 28, 28
img2 = [[0] * 28] * 28
oneimg = []
mnist_dim = 28
multx = width / float(mnist_dim)
multy = height / float(mnist_dim)
xy_list = []
dimx, dimy = img.size
#img = np.asarray(img, dtype='float64')
''' Put in shrunk form. '''
if not len (img.shape) < 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
#print astart, bstart
if True :
item = [ aa, bb, list(img.getpixel((int(astart) ,int(bstart))))]
xy_list.append(item)
''' Put list in 28 x 28 array. '''
if len(xy_list) == 0:
xy_list = [[0, 0,[0,0,0]]]
for i in range(len(xy_list)):
q = xy_list[i]
if i < 10: print(q)
if (q[0] < 28) and (q[1] < 28) and (q[0] >= 0) and (q[1] >= 0) :
img2[int(q[0])] [ int(q[1])] = q[2][0]
''' 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
def print_block(self, img):
#print (img)
for x in range(28):
for y in range(28):
out = str(img[x *28 + y]) +" "
sys.stdout.write(out)
print "|"
print "---------------"
下面列出了一些输出:
[0, 0, [90, 75, 70]]
[0, 1, [85, 77, 66]]
[0, 2, [87, 73, 70]]
[0, 3, [88, 74, 73]]
[0, 4, [86, 73, 64]]
[0, 5, [91, 77, 68]]
[0, 6, [89, 74, 69]]
[0, 7, [86, 73, 65]]
[0, 8, [87, 72, 65]]
[0, 9, [86, 72, 63]]
45 35 48 61 62 61 95 91 94 88 92 93 87 98 178 194 116 98 90 91 92 85 84 88 88 90 91 92 |
45 35 48 61 62 61 95 91 94 88 92 93 87 98 178 194 116 98 90 91 92 85 84 88 88 90 91 92 |
45 35 48 61 62 61 95 91 94 88 92 93 87 98 178 194 116 98 90 91 92 85 84 88 88 90 91 92 |
... more repeated values ...
编辑:这些是我的一些导入
import numpy as np
from PIL import Image
import os
import sys
编辑:我更改了代码并更新了一些输出。我无法理解为什么print (q)
行中的列表与表中的数字不匹配。
答案 0 :(得分:0)
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