Attempting to make an outfit generator but need to generate a random image for the Turtle. I have files full of pictures of clothes, separated into shirts, shoes, and pants. Need the Shirts/Pants/Shoes.register_shape(random_shirt/pants/shoes) to produce a random image from said files. Not sure if this is possible but if it is please let me know. ##If there is a different easier GUI to do this with I'm open to suggestions, currently looking into TKinter.
import random
import turtle
import os
wn = turtle.Screen()
wn.bgcolor('light gray')
wn.setup(width = 600, height = 800)
wn.title('Outfit Generator')
#Turtle of Shirt
Shirt = turtle.Turtle()
Shirt.hideturtle()
Shirt.speed(0)
Shirt.setposition(300, 400)
#Turtle of Pants
Pants = turtle.Turtle()
Pants.hideturtle()
Pants.speed(0)
Pants.setposition(500, 400)
#Turtle of Shoes
Shoes = turtle.Turtle()
Shoes.hideturtle()
Shoes.speed(0)
Shoes.setposition(100, 400)
# Specify the directory where your photos are located
photo_shirts_dir = r'C:\Users\Rest Of Path\Shirts'
photo_pants_dir = r'C:\Users\Rest Of Path\Pants'
photo_shoes_dir = r'C:\Users\Rest Of Path\Shoes'
#Create Directory of all images in files
image_files_shirts = [file for file in os.listdir(photo_shirts_dir) if file.endswith(('.gif', '.jpg', '.jpeg', '.png'))]
image_files_pants = [file for file in os.listdir(photo_pants_dir) if file.endswith(('.gif', '.jpg', '.jpeg', '.png'))]
image_files_shoes = [file for file in os.listdir(photo_shoes_dir) if file.endswith(('.gif', '.jpg', '.jpeg', '.png'))]
if not image_files_shirts:
print("No image files found in the directory.")
else:
random_shirt = (random.choice(image_files_shirts))
turtle.register_shape(random_shirt)
Shirt.shape(random_shirt)
if not image_files_pants:
print("No image files found in the directory.")
else:
random_pants = random.choice(image_files_pants)
turtle.register_shape(random_pants)
Pants.shape(random_pants)
if not image_files_shoes:
print("No image files found in the directory.")
else:
random_shoes = random.choice(image_files_shoes)
turtle.register_shape(random_shoes)
Shoes.shape(random_shoes)
wn.listen()
wn.mainloop()
Error Being Received
Traceback (most recent call last):
File "c:\Users\joezi\OneDrive\Desktop\Python\Outfit Creator\OutfitGenerator.py", line 45, in <module>
turtle.register_shape(random_shirt)
File "<string>", line 8, in register_shape
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.11_3.11.2288.0_x64__qbz5n2kfra8p0\Lib\turtle.py", line 1134, in register_shape
shape = Shape("image", self._image(name))
^^^^^^^^^^^^^^^^^
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.11_3.11.2288.0_x64__qbz5n2kfra8p0\Lib\turtle.py", line 479, in _image
return TK.PhotoImage(file=filename, master=self.cv)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.11_3.11.2288.0_x64__qbz5n2kfra8p0\Lib\tkinter\__init__.py", line 4145, in __init__
Image.__init__(self, 'photo', name, cnf, master, **kw)
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.11_3.11.2288.0_x64__qbz5n2kfra8p0\Lib\tkinter\__init__.py", line 4092, in __init__
self.tk.call(('image', 'create', imgtype, name,) + options)
_tkinter.TclError: couldn't open "shirt.gif": no such file or directory
As discussed in the comments,
os.listdirreturns a list of file names. The error_tkinter.TclError: couldn't open "shirt.gif": no such file or directoryindicates that turtle is trying to openshirt.gifin the current working directory, which isn't right. Try prepending the path back onto the file names:Note I'm using
os.path.join()for an operating-system agnostic path join. This requires addingimport osto the top of your script. I suggest usingos.path.joinfor your other paths as well.Note that your
endswithisn't valid since turtle only works with.gifs at the present time.os.path.splitext("...")[1]is preferable toendswithin any case. The general rule is not to use plain string manipulation on paths to avoid bugs and gotchas.I'd also use a relative path rather than presuming
C:\Users\...is a thing, which it won't be on most computers.Another nitpick: the Python convention is that
PascalCaseis only used for class names. Variables aresnake_case.When attacking problems, it's always good to minimize the problem so the situation is free of any noise, and you can focus fully on the issue.
Directory structure:
test.py:
Error:
Now, the fix:
Related: Reading a file using a relative path in a Python project