Ошибка Pygame: неподдерживаемый формат изображения

Привет, я пытаюсь следовать этим инструкциям из моего последнего вопроса: Pygame: как загрузить 150 изображений и представить каждое всего за 0,5 секунды на пробную версию

Это код, который у меня сейчас есть, и я не уверен, где я ошибаюсь.

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()

person Delly    schedule 15.11.2017    source источник


Ответы (2)


Первая ошибка такая же, как и в вашем предыдущем вопросе: 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)
person skrx    schedule 16.11.2017

Внизу у вас есть строка:

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

Здесь вы пытаетесь загрузить изображение под названием image_list. Там нет расширения файла, поэтому ваша ОС не распознает тип файла. Но даже если вы включили расширение файла, я думаю, вы пытаетесь загрузить отдельные изображения в списке image_list, а это совсем другая история.

person paper man    schedule 15.11.2017
comment
Да, я пытаюсь загрузить отдельные изображения в image_list. Я думал, что индексация сделает это. Есть ли ошибка в том, как я его настроил? - person Delly; 16.11.2017