I'm working on images and its working fine when code is out of function, and gives error when code is inside the function.

My code is:

def matching_image():
  i = 0
  resultlist_key = []
  result_list = list()
  a_list = list()
  b_list = list()
  a_list.append(feature_matrix_ip)# feature_matrix_ip contains features of the query image
  while i < 70:
    b_list.append(feature_matrix_db[i])# feature_matrix_db contains features of img. in DB
    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'])))# sorting according to the least distance and the key will not change
  key = res_lst_srt['keys']

for i1,val in enumerate(key):
    if i1 < 4:
        image_count += 1
        r, c = divmod(image_count, COLUMNS)
        img = Image.fromarray(resizelist[val])
        #im = Image.open(img)
        tkimage1 = ImageTk.PhotoImage(img)
        myvar2 = Label(new, image=tkimage1)
        myvar2.image = tkimage1
        myvar2.grid(row=r, column=c)

This is my code. If i take it outside then it will read and execute. But I want it inside this function only.

The error message what I'm getting is;

%run "D:/6th sem/Major project/Code/frame.py"
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 323, in matching_image
res_lst_srt['values'], res_lst_srt['keys'] = zip(*sorted(zip(res_lst_srt['values'], res_lst_srt['keys'])))

ValueError: need more than 0 values to unpack

Any suggestions, so that I can solve this error.

Thanks in advance!

You have no values in result_list or in resultlist_key.
Try to print out the length of the structures in the matching_image function.

Most likely you have a naming conflict. You have global variables that are recreated at the function level.

Okay, I will check on it. Thanks for tthe suggestion :)

@slate I guess its not the problem with naming. as it is working outside the function, and not working inside the function.

That's why.

An example:

somelist=[]


for i in range(10):
    somelist.append(i)

print somelist

versus

somelist=[]


def process_somelist():
    somelist=[]
    for i in range(10):
        somelist.append(i)

process_somelist()

print somelist

thanks for the suggestion, it helped me !!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.