Hello!
I am an absolute Python beginner.... so maybe someone can help me with my problem:
I have a code where each two images (emblems) of sports teams are compared, and the user has to choose which team is better. In this process, each team competes against each other and the winner gets a point. After all duels are completed, a ranking is output based on the points scored.
This works quite well for a few teams, but when there are more, it gets to a number of duels that is not manageable for one user. Therefore, I would like to change the code to integrate some kind of logic that "unnecessary" duels, where the winner is already determined based on previous results, are omitted and the already determined winner automatically gets a point.
For example: team 1 wins against team 2, team 3 wins against team 4 and team 1 wins against team 3. So it is clear that team 1 also wins against team 4, so this duel should be omitted. The goal is to create a ranking with as few duels as possible. Can someone please help me change the code accordingly?
My code is as follows:
import random
from itertools import combinations
import tkinter as tk
from pathlib import Path
from PIL import ImageTk, Image
FOLDER_PATH = Path("D:/Duels/Images")
class GUI:
def __init__(self):
image_files = list(FOLDER_PATH.iterdir())
self.image_pairs = list(combinations(image_files, 2))
random.shuffle(self.image_pairs)
self.scores = {}
for image in image_files:
self.scores[image.name] = 0
self.selected_images = None
self.root = tk.Tk()
self.root.title("Images")
self.label1 = tk.Label(self.root)
self.label2 = tk.Label(self.root)
self.label1.pack(side=tk.LEFT)
self.label2.pack(side=tk.RIGHT)
self.total_pairs = len(self.image_pairs)
self.progress_frame = tk.Frame(self.root)
self.progress_frame.pack(side=tk.BOTTOM, pady=10)
tk.Label(self.progress_frame, text="Progress:", font=("Arial", 12)).pack(side=tk.LEFT)
self.progress_label = tk.Label(self.progress_frame, text=f"0/{self.total_pairs} (0%)", font=("Arial", 12))
self.progress_label.pack(side=tk.LEFT)
self.root.bind("<Left>", lambda event: self.select_image(0))
self.root.bind("<Right>", lambda event: self.select_image(1))
self.show_next_images()
def select_image(self, number):
self.scores[self.selected_images[number].name] += 1
self.show_next_images()
def show_next_images(self):
if not self.image_pairs:
self.label1['image'] = None
self.label2['image'] = None
print("All image pairs were loaded")
return
self.selected_images = self.image_pairs.pop()
progress = self.total_pairs - len(self.image_pairs)
self.progress_label.config(
text=f"{progress}/{self.total_pairs} ({progress / self.total_pairs:.0%})")
self.load_image(self.label1, self.selected_images[0])
self.load_image(self.label2, self.selected_images[1])
def load_image(self, label, imagefile):
image = Image.open(imagefile)
# resize the image to a maximum of 800x800 while keeping the aspect ratio
max_size = (800, 800)
image.thumbnail(max_size, Image.LANCZOS)
photo = ImageTk.PhotoImage(image)
label['image'] = photo
label.image = photo
def main():
gui = GUI()
gui.root.mainloop()
# Output of the points list
print("Points list:")
for image, score in sorted(gui.scores.items(), key=lambda x: x[1], reverse=True):
print(f"{image}: {score}")
if __name__ == "__main__":
main()