Search code examples
pythonimagedrawpsychopy

Psychopy: draw all images from a list at once


I have a list with 12 images. All of them have different locations (that is, they do not overlap at all). I want to draw them all at once. In other words, I want to see all 12 pictures at the same time on the screen. So far, I only got this:

    lines = [line1,line2,line3,line4,line5,line6
            line7,line8,line9,line10,line11,line12]

    for i in range(12):
        lines[i].draw()

But, of course, this code only draws one pic at a time, after I press any key. Is there then a way to draw the 12 pics at the same time?

Thanks in advance!


Solution

  • Your original code only drew one image at a time because of how the loop was set up -- it was (more or less) saying "For each element in circles, draw several things and flip the front and back buffers". Each time the buffers flip, unless you tell it otherwise via win.flip(clearBuffer = False), the previous things on the screen are removed. To draw the images at the same time, you could just loop through the image list and call the draw() method on each element, e.g.:

    for i in imglist:
        i.draw()
    
    win.flip()
    

    If you are willing to cede control over properties of individual images, one way would be to use BufferImageStim. This takes longer to initialize, but may be faster than drawing individual images (I haven't timed it properly). Both methods are demonstrated below.

    from psychopy import visual, event, core
    import urllib
    import random
    
    win = visual.Window([400, 400], fullscr = False)
    
    # picture of a cat, save to file
    urllib.urlretrieve('https://s-media-cache-ak0.pinimg.com/736x/' + 
                       '07/c3/45/07c345d0eca11d0bc97c894751ba1b46.jpg', 'tmp.jpg')
    
    # create five images with (probably) unique positions
    imglist = [visual.ImageStim(win = win, image = 'tmp.jpg',
                                size = (.2, .2),
                                pos = ((random.random() - 0.5) * 2,
                                       (random.random() - 0.5) * 2))
               for i in xrange(5)]
    
    # draw individual images
    for i in imglist: 
        i.draw()
    
    win.flip()
    
    # wait for key press, then clear window
    event.waitKeys()
    win.flip()
    core.wait(0.5)
    
    # create aggregate stimulus (should look identical)
    buffs = visual.BufferImageStim(win, stim = imglist)
    
    buffs.draw()
    win.flip()
    
    event.waitKeys()
    
    core.quit()