尝试制作服装生成器,但需要为海龟生成随机图像。我的文件里装满了衣服的照片,分为衬衫、鞋子和裤子。需要 Shirts/Pants/Shoes.register_shape(random_shirt/pants/shoes) 从所述文件生成随机图像。不确定这是否可能,但如果可以,请告诉我。 ##如果有一个不同的更简单的 GUI 可以做到这一点,我愿意接受建议,目前正在研究 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()
接收错误
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
正如评论中所讨论的,
os.listdir
返回文件名列表。错误 _tkinter.TclError: couldn't open "shirt.gif": no such file or directory
表示它正在尝试在当前工作目录中打开 shirt.gif
,这是不正确的。尝试将路径重新添加到文件名中:
random_shirt = (random.choice(image_files_shirts))
turtle.register_shape(os.path.join(photo_shirts_dir, random_shirt))
注意,我使用
os.path.join()
进行与操作系统无关的路径连接。这需要将 import os
添加到脚本顶部。我建议您的其他路径也使用 os.path.join
。
请注意,您的
endswith
无效,因为海龟目前仅适用于 .gif
。在任何情况下,os.splitext("...")[1]
都优于endswith
。一般规则是不要在路径上使用纯字符串操作以避免错误和陷阱。