【问题标题】:Pygame Error: Unsupported Image FormatPygame 错误:不支持的图像格式
【发布时间】:2017-11-16 13:37:06
【问题描述】:

您好,我正在尝试按照上一个问题中的这些说明进行操作: Pygame: how to load 150 images and present each one for only .5sec per trial

这是我目前拥有的代码,我不确定哪里出错了。

import pygame, glob
pygame.init()
pygame.mixer.init()
clock=pygame.time.Clock()

#### Set up window
window=pygame.display.set_mode((0,0),pygame.FULLSCREEN)
centre=window.get_rect().center
pygame.mouse.set_visible(False)

#colours
black = (0, 0, 0)
white = (255, 255, 255)


types= '*.tif'
artfile_names= []
for files in types:
    artfile_names.extend(glob.glob(files))

image_list = []
for artwork in artfile_names:
    image_list.append(pygame.image.load(artwork).convert())

##Randomizing
index=0
current_image = image_list[index]

if image_list[index]>= 150:
    index=0

stimulus= pygame.image.load('image_list')
soundPlay=True
def artwork():
    window.blit(stimulus,pygame.FULLSCREEN)

while not soundPlay:
    for imageEvent in pygame.event.get():
        artwork()
        pygame.display.update()
        clock.tick()

【问题讨论】:

    标签: python pygame pygame-surface


    【解决方案1】:

    第一个错误与您的previous question 相同:types= '*.tif'。这意味着types 是一个字符串,但您实际上想要一个具有允许文件类型的元组:types = '*.tif',(逗号将其转换为一个元组)。因此,您遍历'*.tif' 中的字母并将它们传递给glob.glob,这将为您提供目录中的所有文件,当然image_list.append(pygame.image.load(artwork).convert()) 不能工作,如果您将它传递给例如.py 文件。

    下一个错误是stimulus = pygame.image.load('image_list') 行不起作用,因为您需要将完整的文件名或路径传递给load 函数。我认为您的stimulus 变量实际上应该是current_image

    这是一个完整的示例,它还向您展示了如何实现计时器。

    import glob
    import random
    import pygame
    
    
    pygame.init()
    clock = pygame.time.Clock()
    window = pygame.display.set_mode((640, 480))
    
    file_types = '*.tif',  # The comma turns it into a tuple.
    # file_types = ['*.tif']  # Or use a list.
    artfile_names = []
    for file_type in file_types:
        artfile_names.extend(glob.glob(file_type))
    
    image_list = []
    for artwork in artfile_names:
        image_list.append(pygame.image.load(artwork).convert())
    
    random.shuffle(image_list)
    
    index = 0
    current_image = image_list[index]
    previous_time = pygame.time.get_ticks()
    
    done = False
    
    while not done:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                done = True
    
        # Calcualte the passed time, then increment the index, use
        # modulo to keep it in the correct range and finally change
        # the image.
        current_time = pygame.time.get_ticks()
        if current_time - previous_time > 500: # milliseconds
            index += 1
            index %= len(image_list)
            current_image = image_list[index]
            previous_time = current_time
    
        # Draw everything.
        window.fill((30, 30, 30))  # Clear the screen.
        # Blit the current image.
        window.blit(current_image, (100, 100))
    
        pygame.display.update()
        clock.tick(30)
    

    【讨论】:

      【解决方案2】:

      在底部附近有一行:

      stimulus= pygame.image.load('image_list')
      

      您正在尝试加载标题为image_list 的图像。那里没有文件扩展名,因此您的操作系统无法识别文件类型。但即使您确实包含了文件扩展名,我认为您正在尝试加载列表中的单个图像image_list,这是另一回事。

      【讨论】:

      • 是的,我正在尝试在 image_list 中加载单个图像。我认为索引会做到这一点。我的设置是否有错误?
      猜你喜欢
      • 2021-12-20
      • 2016-07-28
      • 1970-01-01
      • 2017-04-03
      • 1970-01-01
      • 1970-01-01
      • 2014-06-29
      • 1970-01-01
      • 2021-11-05
      相关资源
      最近更新 更多