我将 150 张 .tif 图像加载到 glob 模块,但目前无法加载它们。我对编程很陌生,所以我想这是我遗漏的一个愚蠢的错误,但我似乎无法弄清楚。
这是代码:
import pygame, glob
types= ('*.tif')
artfile_names= []
for files in types:
artfile_names.extend(glob.glob(files))
print(artfile_names)
for artworks in artfile_names:
pygame.image.load(str(artfile_names))
感谢您的帮助!
最佳答案
错误是你的 types
变量只是一个字符串(用括号括起来没有任何效果),所以你遍历字符串中的字母并调用 artfile_names.extend(glob .glob(files))
每个字母。
逗号构成元组(空元组除外):
types = '*.tif', # This gives you a tuple with the length 1.
types = '*.tif', '*.png' # This is a tuple with two elements.
在代码的第二部分,您需要遍历 artfile_names
,调用 pygame.image.load(artwork)
从您的硬盘加载图像并附加结果 surface到列表:
images = []
for artwork in artfile_names:
images.append(pygame.image.load(artwork).convert())
调用.convert()
方法(或.convert_alpha()
用于透明图像)以提高blit性能。
关于python - Pygame:无法打开文件。同一目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47244198/