psychopy.org | Reference | Downloads | Github

Displaying image sequentially and then moving back and forth

Hi all,

I have ten images and want to display (at the centre of the screen) these images sequentially one after another by pressing ‘right’ arrow and then wish to go back by pressing ‘left’ arrow to images from 10 to 1.

I have written following PsychoPy code. I can display images 1 to 10 by pressing ‘right’ arrow button but when I want to go back 10 to 1 code does not work - any help?:

Here is my code:

temp = ['F1.jpg', 'F2.jpg', 'F3.jpg', 'F4.jpg', 'F5.jpg', 'F6.jpg', 'F7.jpg', 'F8.jpg', 'F9.jpg', 'F10.jpg']

images = []
images = [0 for i in range(len(temp))]
for i in range(len(temp)):
    tmp = temp[i]
    images[i] = visual.ImageStim(win, image=tmp, pos=(0, 0), size=(0.4, 0.4))

current_image = 0
continueRoutine = True
# start the loop to display images
while continueRoutine: 
    keyP = event.getKeys() # Get the key pressed
  
    if keyP:  # if there was an actual key pressed:
        if keyP[0] == 'left':
            current_image -= 1
            if current_image <= 0: # Don't want index below zero
                current_image = 0

        elif keyP[0] == 'right':
            current_image += 1
            if current_image >= 9: # Don't want index more than 9
                current_image = 9

    images[current_image].setAutoDraw(True)
    win.flip(True) 

    # quit (the Esc key)
    if defaultKeyboard.getKeys(keyList=["escape"]):
        core.quit()

win.close()
core.quit()

Hi @wuaham, if “it does not work” means that your images stay on the first or last image as your progress to the beginning or end of the image carousel, then try:

if keyP:  # if there was an actual key pressed:
    if keyP[0] == 'left':
        current_image -= 1
        if current_image < 0: # If index below zero, go to end
            current_image = 9

    elif keyP[0] == 'right':
        current_image += 1
        if current_image > 9: # If index > 9, go to beginning
            current_image = 0

Many thanks for the suggestion, I replaced my as of advice, unfortunately this is also not working…!

Ok, thanks. I will need more information about why it is not working. It would be helpful if you could describe what you are trying to achieve, and what is happening / why it is not working.

I am working on Win-10 PC PsychoPy3 (latest version). I am trying to display F1.jpg first then if I press ‘right’ arrow button in my keyboard it display F2.jpg and then press again F3.jpg, and so on… reach to F10.jpg

Now I press ‘left’ arrow button to display back to F9.jpg then again left to display F8.jpg, but it does not display me anything but stuck at F10.jpg

Moreover, my objective is to go back and forth from and image to any image (next <–>previous) at any time I wish.

Any help would be appreciated!
Thank you.

Thanks, here is a simple example using Builder. We use an image, keyboard and code component. Replace the images in the temp variable with the names of your own images, and it should work.

# Begin Experiment
temp = ["one.png", "two.png", "three.png"]  # image list
maxIndex = len(temp) - 1  # max image index

# Begin Routine
currentPos = 0

# Each Frame


if len(key_resp.keys):  # If keypress recorded
    if key_resp.keys == 'left':
        currentPos -= 1
        if currentPos < 0:
            currentPos = maxIndex  # Set position to end
    elif key_resp.keys == 'right':
        currentPos += 1
        if currentPos > maxIndex:
            currentPos = 0  # Set position to beginning

    # Reset keypresses
    key_resp.keys = []
    key_resp.rt = []

# Set the image to the new image
if image.image != temp[currentPos]:
    image.setImage(temp[currentPos])

imCar.psyexp (7.3 KB)

Thank you. I tried to run the code changing image file names and got the following error:

imCar_lastrun.py, line 135, in
if len(key_resp.keys):
NameError: name ‘key_resp.keys’ is not defined

Thanks. Did you run the actual Builder file, or just copy the code?

Yeap, actual builder file, just changing the image file names
Thank you.

Ok, I think perhaps you are using an older version of PsychoPy that I am, and the keyboard is changing names. Try the attached instead

imCar.psyexp (7.3 KB)

Great job! Finally working… thank you.