我将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)
以从硬盘加载图像并将生成的表面附加到列表中:
images = []
for artwork in artfile_names:
images.append(pygame.image.load(artwork).convert())
调用该.convert()
方法(或.convert_alpha()
用于具有透明性的图像)以提高blit性能。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句