A csv file continues per row several filenames of images. These images are visualised in a grid
of tkinter
. Actually the visualisation has the same format (position of filename of images) as the csv.
Within my csv file I'm trying to look for matching filename per each row. If they match per row a green border should be drawn. If not, a red border is added.
An example how my csv file (in the code named results.csv
) might look:
a.jpg, b.jpg, a.jpg, c.jpg, d.jpg
x.jpg, y.jpg, y.jpg, y.jpg, z.jpg
As I described above and for this example, I want to draw for the first row green borders around a.jpg
and for the second row around the image x.jpg
, since these are images per row, which match.
According to this logic, my code looks like this:
def visualize_results(feature):
"""Visualize result images in a grid like the original gui"""
with open("results.csv", 'r') as input:
reader = csv.reader(input, delimiter=',')
i = 0
for row in reader:
for j, col in enumerate(row):
tk.Label(text="", width=25).grid(row=0, column=j)
# dataset is a path to a dataset
# and col is the filename of an image in the csv file (as in the example a.jpg...)
image = Image.open(dataset + col)
image = image.resize((50, 50), Image.ANTIALIAS)
photo = ImageTk.PhotoImage(image)
# compare every id in each row and if same, draw a green border
if col in row[i]:
label = tk.Label(image=photo, width=50, height=50,
highlightthickness=4, highlightbackground="GREEN", borderwidth=0)
else:
label = tk.Label(image=photo, width=50, height=50,
highlightthickness=4, highlightbackground="RED", borderwidth=0)
label.image = photo # this line need to prevent gc
label.grid(row=i + 1, column=j)
i += 1
tk.mainloop()
Now with that code I get the exception IndexError: list index out of range
. Why is that so and how can I change it?
It is a bit unclear if you want to mark all rows where there are any duplicates or if you want to search for the first entry in the rest of the row.
The problem of searching for duplicates in a list is easy to solve with
list.count()
If you want to see if the first element is in the rest of the row you can slice the the rest of the row in the comparison