'numpy.ndarray'对象没有属性'read' - python

我正在尝试将函数中声明的变量用于另一个函数。但是,当我这样做时,我得到了这样的错误:

Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\HP\AppData\Local\Enthought\Canopy32\App\appdata\canopy-1.0.3.1262.win-x86\lib\lib-tk\Tkinter.py", line 1410, in __call__
return self.func(*args)
File "D:\6th sem\Major project\Code\frame.py", line 198, in result
im = Image.open(resizelist[val])
File "E:\Canopy\System\lib\site-packages\PIL\Image.py", line 1956, in open
prefix = fp.read(16)
AttributeError: 'numpy.ndarray' object has no attribute 'read'

我的代码是:

def messageWindow():
  win = Toplevel()
  path = 'C:\Users\HP\Desktop\dataset'
  COLUMNS = 12
  image_count = 0
  for infile in glob.glob(os.path.join(path, '*.jpg')):
    image_count += 1
    r, c = divmod(image_count, COLUMNS)
    im = Image.open(infile)
    resized = im.resize((100, 100), Image.ANTIALIAS)
    tkimage = ImageTk.PhotoImage(resized)
    myvar = Label(win, image=tkimage)
    myvar.image = tkimage
    myvar.grid(row=r, column=c)
  i=0
  cont_list = list()
  ene_list = list()
  homo_list = list()
  cor_list = list()
  dis_list = list()
  B_mean = list()
  G_mean = list()
  R_mean = list()
  piclist = list()
  graylist = list()
  resizelist = list()
  eq_graylist = list()

 for infile in glob.glob(os.path.join(path,'*.jpg')):
    imge = cv2.imread(infile)
    arr = array(imge)
    piclist.append(imge)

    g_img = cv2.imread(infile,0)
    gray_re_img = cv2.resize(g_img,(256,256))
    graylist.append(gray_re_img)

    equ = cv2.equalizeHist(gray_re_img)
    eq_graylist.append(equ)

    re_img = cv2.resize(imge,(256,256))
    resizelist.append(imge)
    i = i + 1

 for infiles in glob.glob(os.path.join(path,'*.jpg')):
    img = cv2.imread(infiles)
    blue, green, red = cv2.split(img)
    total = img.size
    B = sum(blue) / total
    G = sum(green) / total
    R = sum(red) / total
    B_mean.append(B)
    G_mean.append(G)
    R_mean.append(R)

    im = skimage.io.imread(infile, as_grey=True)
    im = skimage.img_as_ubyte(im)
    im /= 32
    g = skimage.feature.greycomatrix(im, [1], [0], levels=8, symmetric=False, normed=True)
    cont = skimage.feature.greycoprops(g, 'contrast')[0][0]
    cont_list.append(cont)
    ene = skimage.feature.greycoprops(g, 'energy')[0][0]
    ene_list.append(ene)
    homo = skimage.feature.greycoprops(g, 'homogeneity')[0][0]
    homo_list.append(homo)
    cor = skimage.feature.greycoprops(g, 'correlation')[0][0]
    cor_list.append(cor)
    dis = skimage.feature.greycoprops(g, 'dissimilarity')[0][0]
    dis_list.append(dis)

 feature_matrix_db = zip( B_mean , G_mean , R_mean, cont_list , ene_list , homo_list , cor_list, dis_list)
 blue2.set(B_mean)
 green2.set(G_mean)
 red2.set(R_mean)
 con2.set(cont_list)
 ene2.set(ene_list)
 homo2.set(homo_list)
 corr2.set(cor_list)
 diss2.set(dis_list)        
 return(feature_matrix_db,resizelist)

def OPEN():
  path=tkFileDialog.askopenfilename(filetypes=[("Image File",'.jpg')])
  custName.set(path)
  im = Image.open(path)
  resized = im.resize((200, 200),Image.ANTIALIAS)
  tkimage = ImageTk.PhotoImage(resized)
  myvar=Label(root,image = tkimage)
  myvar.image = tkimage
  myvar.pack()
  myvar.place(x = 30, y = 100) 
  graylist1 = list()
  resizelist1 = list()
  eq_graylist1 = list()
  cont_list1 = list()
  ene_list1 = list()
  homo_list1 = list()
  cor_list1 = list()
  B_mean1 = list()
  G_mean1 = list()
  R_mean1 = list()
  dis_list1 = list()

  imge = cv2.imread(path)
  arr = array(imge)
  g_img = cv2.imread(path,0)
  gray_re_img = cv2.resize(g_img,(256,256))
  graylist1.append(gray_re_img)

  equ = cv2.equalizeHist(gray_re_img)
  eq_graylist1.append(equ)

  re_img = cv2.resize(imge,(256,256))
  resizelist1.append(re_img)

  blue, green, red = cv2.split(re_img)
  total = re_img.size
  B = sum(blue) / total
  G = sum(green) / total
  R = sum(red) / total
  B_mean1.append(B)
  G_mean1.append(G)
  R_mean1.append(R)

  im = skimage.io.imread(path, as_grey=True)
  im = skimage.img_as_ubyte(im)
  im /= 32
  g = skimage.feature.greycomatrix(im, [1], [0], levels=8, symmetric=False, normed=True)
  cont = skimage.feature.greycoprops(g, 'contrast')[0][0]
  cont_list1.append(cont)
  ene = skimage.feature.greycoprops(g, 'energy')[0][0]
  ene_list1.append(ene)
  homo = skimage.feature.greycoprops(g, 'homogeneity')[0][0]
  homo_list1.append(homo)
  cor = skimage.feature.greycoprops(g, 'correlation')[0][0]
  cor_list1.append(cor)
  dis = skimage.feature.greycoprops(g, 'dissimilarity')[0][0]
  dis_list1.append(dis)

 feature_matrix_ip = zip( B_mean1 , G_mean1 , R_mean1, cont_list1 , ene_list1 , homo_list1 , cor_list1 , dis_list1)
 blue1.set(B_mean1)
 green1.set(G_mean1)
 red1.set(R_mean1)
 con1.set(cont_list1)
 ene1.set(ene_list1)
 homo1.set(homo_list1)
 corr1.set(cor_list1)
 diss1.set(dis_list1)
 return(feature_matrix_ip)

def result():
  COLUMNS = 12
  image_count = 0
  resultlist_key = []
  result_list = list()
  i = 0
  a_list = list()
  b_list = list()
  a_list.append(feature_matrix_ip)
  while i < 70:
     b_list.append(feature_matrix_db[i])
     dist = distance.euclidean(a_list,b_list[i])
     result_list.append(dist)
     resultlist_key = OrderedDict(sorted(enumerate(result_list),key=lambda x: x[0])).keys()
     i = i + 1 

 res_lst_srt = {'values': result_list,'keys':resultlist_key}
 res_lst_srt['values'], res_lst_srt['keys'] = zip(*sorted(zip(res_lst_srt['values'], res_lst_srt['keys'])))
 key = res_lst_srt['keys']

 for i1,val in enumerate(key):
     if i1 < 4:
         image_count += 1
         r, c = divmod(image_count, COLUMNS)
         im = Image.open(resizelist[val]) # <---- This is where the error is coming
         tkimage = ImageTk.PhotoImage(resized)
         myvar = Label(win, image=tkimage)
         myvar.image = tkimage
         myvar.grid(row=r, column=c) 

即使在return(feature_matrix_db, resizelist)之后,它也会给出相同的错误。有什么办法可以解决这个问题?还是我需要更改代码。我已初始化的所有内容。每个必需的标头都被调用/导入。

提前致谢!

参考方案

所以从http://effbot.org/imagingbook/image.htm

Image.open(文件)⇒图片

Image.open(文件,模式)⇒图像

打开并标识给定的图像文件。这是一个懒惰的操作;
该函数读取文件头,但实际的图像数据不是
从文件中读取数据,直到尝试处理数据为止(调用负载
强制加载的方法)。如果给出了mode参数,则必须为
“ r”。

您可以使用字符串(代表文件名)或文件
对象作为文件参数。在后一种情况下,文件对象必须
实现读取,查找和告诉方法,并以二进制模式打开。

来自PIL导入的图像im =来自PIL导入的Image.open(“ lenna.jpg”)
从StringIO导入ImageIO的图像

从字符串im = Image.open(StringIO(data))读取数据

如文档所述,传递给Image.open的参数必须实现readseektell方法。当传递一个由OpenCv生成的numpy数组时,它需要一个文件名,StringIO实例或文件对象。

我认为您可以用Image.open替换有问题的Image.fromarray调用,这将以numpy数组作为输入。即:

im = Image.fromarray(resizelist[val])

如何修复AttributeError:模块'numpy'没有属性'square' - python

Improve this question 我已经将numpy更新为1.14.0。我使用Windows10。我尝试运行我的代码,但出现此错误: AttributeError:模块“ numpy”没有属性“ square”这是我的进口商品:%matplotlib inline import matplotlib.pyplot as plt import ten…

AttributeError:'AnonymousUserMixin'对象没有属性'can' - python

烧瓶学习问题为了定制对匿名用户的要求,我在模型中设置了一个类: class MyAnonymousUser(AnonymousUserMixin): def can(self, permissions): return False def is_administrator(self): return False login_manager.anonymous…

numpy.savetxt“元组索引超出范围”? - python

我试图在文本文件中写几行,这是我使用的代码:import numpy as np # Generate some test data data = np.arange(0.0,1000.0,50.0) with file('test.txt', 'w') as outfile: outfile.write('…

Python panda read_csv在导入问题期间使用数据中的“-”值转换数据 - python

我一直在努力解决这个问题。我终于找到了发生这种情况的原因,但还找不到解决方案。我正在导入从网络上其他资源中抓取的data.csv。它们的大部分是字符串,需要除去,例如“%”。使用自定义转换器时,它的工作原理就像一种魅力。但是,只要一列包含带有“-”(无值)的行,它就会在convert_percentage中显示错误“ File“ D:test.py”,第14…

在返回'Response'(Python)中传递多个参数 - python

我在Angular工作,正在使用Http请求和响应。是否可以在“响应”中发送多个参数。角度文件:this.http.get("api/agent/applicationaware").subscribe((data:any)... python文件:def get(request): ... return Response(seriali…