Как перетаскивать несколько изображений, перебирая список имен файлов? - PullRequest
0 голосов
/ 23 мая 2018

Я пытаюсь скопировать несколько изображений движущегося автомобиля, используя цикл for для имен файлов изображений.Тем не менее, он просто рисовал бы экран, но не отображал / не отображал изображения.Я использую python3.6.

Вот мой код.

import pandas as pd
import pygame

# BLACK = (  0,   0,   0)
# WHITE = (255, 255, 255)
# BLUE =  (  0,   0, 255)
# GREEN = (  0, 255,   0)
# RED =   (255,   0,   0)

df = pd.read_csv('./result.csv')

preds = df['Predicted Angles']
true = df['Actual Angles']
filenames = df['File']

pygame.init()
size = (640, 320)
pygame.display.set_caption("Data viewer")
screen = pygame.display.set_mode(size, pygame.DOUBLEBUF)
myfont = pygame.font.SysFont("monospace", 15)

for i in range(len(list(filenames))):
    img = pygame.image.load(filenames.iloc[i])
    screen.blit(img, (0, 0))
    pygame.display.flip()

Просмотр результатов.csv

1 Ответ

0 голосов
/ 23 мая 2018

Сначала загрузите все изображения и поместите их в список или другую структуру данных, затем назначьте текущее изображение переменной и измените его после желаемого интервала времени (вы можете использовать один из этих таймеров ).

Я просто использую некоторые цветные pygame.Surfaces в примере ниже и изменяю текущее изображение / поверхность с помощью пользовательского события и функции pygame.time.set_timer, которая добавляет событиев очередь событий по истечении указанного времени.

import pygame as pg


pg.init()
screen = pg.display.set_mode((640, 480))
clock = pg.time.Clock()
BG_COLOR = pg.Color('gray12')

images = []
# Three differently colored surfaces for demonstration purposes.
for color in ((0, 100, 200), (200, 100, 50), (100, 200, 0)):
    surface = pg.Surface((200, 100))
    surface.fill(color)
    images.append(surface)

index = 0
image = images[index]
# Define a new event type.
CHANGE_IMAGE_EVENT = pg.USEREVENT + 1
# Add the event to the event queue every 1000 ms.
pg.time.set_timer(CHANGE_IMAGE_EVENT, 1000)

done = False
while not done:
    for event in pg.event.get():
        if event.type == pg.QUIT:
            done = True
        elif event.type == CHANGE_IMAGE_EVENT:
            # Increment the index, use modulo len(images)
            # to keep it in the correct range and change
            # the image.
            index += 1
            index %= len(images)
            image = images[index]  # Alternatively load the next image here.

    screen.fill(BG_COLOR)
    # Blit the current image.
    screen.blit(image, (200, 200))
    pg.display.flip()
    clock.tick(30)

pg.quit()
...