Re: [psychopy-users] Preload images

1,141 views
Skip to first unread message

Jeremy Gray

unread,
Apr 28, 2013, 10:15:36 AM4/28/13
to psychop...@googlegroups.com
Hi Andre,

40 seconds is a small eon. Even a slowly-loading image should not take more than ~1 second to load, typically less. And for obtaining ratings, its ok to have that kind of delay, so I don't think you really need to pre-load. So something else is going on here. What are the dimensions of your images? What is your script like--how is it loading things, etc? 

--Jeremy


On Sun, Apr 28, 2013 at 6:39 AM, Andre L. Souza <andre.s...@gmail.com> wrote:
Hi,

I know this might be a very basic question but I looked online and couldn't find an answer. So maybe someone can help!

I have compiled an experiment (using Builder v.1.76.00) in which I will be showing participants a lot of images (and they'll be rating them). Even though the images (.png) are not very large, the experiment is running extremely slow (sometimes an image takes up to 40 seconds to load). Searching online, I found that is best to preload all images prior to running the experiment, so that the images don't need to be called "live". However, how do I do that? How do I preload all the images that will be used in the experiment, so that the routines don't run super slow?

Thanks,

A.

--
You received this message because you are subscribed to the Google Groups "psychopy-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to psychopy-user...@googlegroups.com.
To post to this group, send email to psychop...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msg/psychopy-users/-/q49jz_aQiPgJ.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Andre L. Souza

unread,
Apr 28, 2013, 11:58:02 PM4/28/13
to psychop...@googlegroups.com
Hi Jeremy,

Thanks for your reply! I'm sure that is something else going on! I just tried to run the experiment in my computer now and the first image took over 2 minutes to load (and my computer crashed by the third one). I'll try to run the experiment in a different computer and see what happens. It might just be my old Mac not able to handle something.

The images are 1280 × 800 (~50kb each). I'm presenting about 80 images.

The delay is not a big problem for the ratings. The problem is that I set up the trial to end when the participant types his answer. However, the participant types the answer, the picture disappears and the next one takes a loooong time to show up (giving the impression that the participant has to do something or that the computer is frozen).

I'm not very savy programming wise, so what should I look at in the script in terms of how it is loading things?

thanks again for the help,

A.

Jeremy Gray

unread,
Apr 29, 2013, 12:02:46 AM4/29/13
to psychop...@googlegroups.com
2 minutes for one image?! oh dear, something is really not going well. let us know if another computer works better, and we'll take it from that point.




To view this discussion on the web visit https://groups.google.com/d/msg/psychopy-users/-/g0egra7vkQ4J.

Andre L. Souza

unread,
Apr 29, 2013, 12:12:15 AM4/29/13
to psychop...@googlegroups.com
Yeah! I'll let you know! :)
Thanks

Andre L. Souza

unread,
Apr 29, 2013, 7:54:28 PM4/29/13
to psychop...@googlegroups.com
Jeremy,

It ran perfectly on other two machines! :) I guess the problem was my old MacBook!
Thanks!
A.


Le lundi 29 avril 2013 00:02:46 UTC-4, Jeremy a écrit :

Jeremy Gray

unread,
Apr 29, 2013, 10:14:37 PM4/29/13
to psychop...@googlegroups.com
glad to hear you have a working solution.


To view this discussion on the web visit https://groups.google.com/d/msg/psychopy-users/-/fkeTTlY7n8oJ.

kathrin

unread,
Sep 23, 2013, 5:31:00 AM9/23/13
to psychop...@googlegroups.com

Hi,
I am having a similar problem, however on a totally new MacPro. To preload the images I simply put them in a visual.ImageStim or is there a way to put all the stimuli in an array at the beginning of the experiment.
Thank you in advance,
Cheers,
kathrin

kathrin

unread,
Sep 23, 2013, 7:58:09 AM9/23/13
to psychop...@googlegroups.com

ok, I have found a way to put them all in a lsit beforehand,
Cheers,
kathrin

Max Curran

unread,
Jan 2, 2014, 5:03:30 PM1/2/14
to psychop...@googlegroups.com
Hi Kathrin,

I'm having a similar problem - trying to load images to be displayed every one second, and getting about a 4 second total lag over ~400 images or so - could you share with me how you were able to preload them like this?

Thanks!

Max

kathrin

unread,
Jan 3, 2014, 9:56:56 AM1/3/14
to psychop...@googlegroups.com

Hi Max,
 
my way is probably not the shortest-but it works. What I did was putting the picture names in a .txt file (made by matlab to randomize them) and then read them in a list in psychopy to present them.
 
without the pulling from a .txt, it would be in psychopy:
 
use_pic=() # empty tuple
use_pic=use_pic+('picture1.png',)+('picture2.png',) #and so on
 
then I put them in a list, which made it easier for me to present them (still I am unsure whether it is necessary):
 
 
pictures=[] # empty list
pictures.append(use_pic)
 
 
Hope this helps,
Cheers,
kathrin

Jeremy Gray

unread,
Jan 3, 2014, 10:37:35 AM1/3/14
to psychop...@googlegroups.com
Thanks for posting Kathrin. If your approach works for you, that's
really great, nothing more is needed. There are typically multiple
ways to achieve the same goal. I have a couple comments for other
folks, and don't at all intend to critique your posting.

Max's question was about preloading, which happens in
visual.ImageStim() initialization. Also, a quick note to say that its
easy to randomize in python, matlab is not needed for that step. (It
can work to use matlab, but its not necessary.)

Max, I would suggest having all your images be contained in a single
directory, e.g., named "stim" (no quotes), that is within your
experiment directory. Have all of the image files be inside "stim".
The following example assumes all images are of type .png

# basic things needed:
from psychopy import visual
import os, glob, random
win = visual.Window()

# the actual interesting code lines:
imgList = glob.glob(os.path.join('stim', '*.png')) # reads all png
file names from directory
random.shuffle(imgList) # or sort how you want them
pictures = [visual.ImageStim(win, img) for img in imgList] # preloads

Then you can loop through pictures[0], pictures[1], etc. These refer
to objects of type ImageStim, so you can do things like:

trial = 42
pictures[trial].draw()
--Jeremy
> --
> You received this message because you are subscribed to the Google Groups
> "psychopy-users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to psychopy-user...@googlegroups.com.
> To post to this group, send email to psychop...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/psychopy-users/722fd590-5a2a-4b48-bc18-b759bb3c6501%40googlegroups.com.

Max Curran

unread,
Jan 7, 2014, 4:33:38 PM1/7/14
to psychop...@googlegroups.com
Hi Jeremy - thanks this seems really useful, I'll give it a shot.

I'm using builder mode and the images are currently in a conditions file… so to display the image would I put something like $pictures[trial] in the "Image" part of an image component?

Jeremy Gray

unread,
Jan 7, 2014, 5:58:50 PM1/7/14
to psychop...@googlegroups.com
> I'm using builder mode and the images are currently in a conditions file… so
> to display the image would I put something like $pictures[trial] in the
> "Image" part of an image component?

I think that's exactly right.

--Jeremy

Dan Dillon

unread,
Jan 9, 2014, 7:33:01 PM1/9/14
to psychop...@googlegroups.com
Hi Jeremy. I'm having a problem that seems relevant to this thread . . . I've got 120 png files of clip-art, with dimensions no bigger than 600 x 600 pixels and filesizes typically 100kB or less. I'm running an fMRI study so I need precise timing, and I've gotten that by (1) pre-loading the images into a dict that I iterate through over my trials and (2) calling "clearTexture()" at the start of each trial, before I call that trial's image from the dict. The problem is that on trial 120--i.e., the last trial--the image does not display correctly. I can see its outline, but other than that nothing appears. Because I can see the outline, and because the image name gets written out to a csv file, I know that PsychoPy is "trying" to display it . . . but I can't get it to actually show up! If I display the image names instead of the images themselves, all 120 names appear so I don't think there's some really basic error with my indexing. Any ideas? I can send you my code if that would help.

Thanks!

Dan  

Jeremy Gray

unread,
Jan 9, 2014, 9:57:26 PM1/9/14
to psychop...@googlegroups.com
Hmmm, now I think that's not right. That would be giving an ImageStim
object instead of the name of a file to load as an image stim.
Instead, I think there are two possibilities:

1.
Leave the "Image" part of the image component blank. Have a Code
component above the Image comp (higher on the panel within the
routine), and in the Begin routine section, put the code:

image_2 = pictures[trial]

This assumes that the image component you made is named "image_2".
This is a bit sneaky because its replacing the ImageStim that was
created by the Image component with the ImageStim that you preloaded.

2.
Have you tried loading during an ISI period? This might actually be
the best overall solution. Instead of constant, change the image
display to be "set during: trial.ISI"

--Jeremy

Jeremy Gray

unread,
Jan 9, 2014, 10:10:23 PM1/9/14
to psychop...@googlegroups.com
Hi Dan,

Does that last image will display correctly on its own, e.g., if it
comes first in the series rather than last?

I'm not sure what clearTexture() is intended to do at the start of
each trial. I would try without that. I would use clearTextures() when
you want to free up some memory prior to loading another image from
disk. But if they are all preloaded, I don't see what it will
accomplish here.

--Jeremy
> --
> You received this message because you are subscribed to the Google Groups
> "psychopy-users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to psychopy-user...@googlegroups.com.
> To post to this group, send email to psychop...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/psychopy-users/8c24ded6-7929-4e34-823e-b8a1d7b8e9fd%40googlegroups.com.

Dan Dillon

unread,
Jan 9, 2014, 11:45:06 PM1/9/14
to psychop...@googlegroups.com
Hi Jeremy, and thanks for your quick reply. I randomize the order of the images each time before I load them into the dict, so the problem is definitely with the position rather than with a particular image (i.e., the images all display fine as long as they're not randomized to the last position). With regard to the clearTexture(), based on some timing tests that I've run it seems to be necessary . . . I thought pre-loading would make it redundant, too, but if I just comment that "clearTexture" line out and run the code (with no other changes), I'm recording image display durations that are roughly 200-500 ms slower than if I leave it in. It certainly seems like there are some memory issues here, but my hunch is that there is some way for me to get around this "last image" display problem without altering my hardware. I'm on a MacBook Pro laptop by the way, running OS 10.6.8.

Dan


You received this message because you are subscribed to a topic in the Google Groups "psychopy-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/psychopy-users/kw5qRatA9f0/unsubscribe.
To unsubscribe from this group and all its topics, send an email to psychopy-user...@googlegroups.com.

To post to this group, send email to psychop...@googlegroups.com.

Jonathan Peirce

unread,
Jan 10, 2014, 5:10:26 AM1/10/14
to psychop...@googlegroups.com
The way you wrote your post it sounds like you're trying to load all of the images at once before presenting any of them.

If you upload an image of 600x600 it will (I think) take 1024x1024x3 bytes on the graphics card (because texture memory is organised in square powers of 2 and you've gone beyond 512) so you're trying to occupy about 3Mb per image, and you might well not have the necessary 350Mb of texture memory to store all of them at once.

What you should ideally do is preload one image at a time, while the previous one is being presented. This is now possible even in the Builder using a Static component (like the ISI component that shows up by default in the trial routine). Rather than update "Each Repeat" you set it to update "During trial.ISI" (or any static period you like).

Jon
>> > file� so

>> > to display the image would I put something like $pictures[trial] in the
>> > "Image" part of an image component?
>>
>> I think that's exactly right.
>>
>> --Jeremy
>
> --
> You received this message because you are subscribed to the Google Groups
> "psychopy-users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to psychopy-user...@googlegroups.com.
> To post to this group, send email to psychop...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/psychopy-users/8c24ded6-7929-4e34-823e-b8a1d7b8e9fd%40googlegroups.com.
>
> For more options, visit https://groups.google.com/groups/opt_out.

--
You received this message because you are subscribed to a topic in the Google Groups "psychopy-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/psychopy-users/kw5qRatA9f0/unsubscribe.
To unsubscribe from this group and all its topics, send an email to psychopy-user...@googlegroups.com.
To post to this group, send email to psychop...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/psychopy-users/CANqwJFh4cA385odLce3JbjGgM6Dk5BcTQGCnHeVw2qx_7joC3Q%40mail.gmail.com.
--
You received this message because you are subscribed to the Google Groups "psychopy-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to psychopy-user...@googlegroups.com.
To post to this group, send email to psychop...@googlegroups.com.

For more options, visit https://groups.google.com/groups/opt_out.

-- 
Jonathan Peirce
Nottingham Visual Neuroscience

http://www.peirce.org.uk

This message and any attachment are intended solely for the addressee and may contain confidential information. If you have received this message in error, please send it back to me, and immediately delete it.   Please do not use, copy or disclose the information contained in this message or in any attachment.  Any views or opinions expressed by the author of this email do not necessarily reflect the views of the University of Nottingham.

This message has been checked for viruses but the contents of an attachment may still contain software viruses which could damage your computer system, you are advised to perform your own checks. Email communications with the University of Nottingham may be monitored as permitted by UK legislation.


Dan Dillon

unread,
Jan 10, 2014, 7:23:16 PM1/10/14
to psychop...@googlegroups.com
Hi Jon. You're right, I was pre-loading all the images at once (to avoid timing slow-downs), and when I load them one at a time they all display fine. I'm very glad to hear I can avoid timing slow-downs by pre-loading during a static period, but I'm having trouble getting that to work. I'm using the Coder v1.76.00, and I'm getting an "ImportError: cannot import name StaticPeriod" message. Do I need a later version of PsychoPy? If so, can you point me to the version I'd need and some installation instructions? I've got the latest "standalone" version and I'm not sure how to install the source distributions (I downloaded 1.77.01 and tried "run setup.py" in IPython, but that returned an error).

Thanks in advance, and thanks also for this great software! I've just started using Python and am enjoying working with PsychoPy.

Dan


>> > file… so

Jonathan Peirce

unread,
Jan 12, 2014, 6:51:50 AM1/12/14
to psychop...@googlegroups.com
If you're using coder (I assumed you were on Builder) then you can do this manually, without the need for a static period as such. The logic (pseudo code) is like this:

#sometime before first image presentation:
stim.setImage(imageList[0])

for imageName in imageList[1:]: #loop through other images
��� stim.draw()
��� win.flip()
��� tStart = clock.getTime() #the precise moment the stimulus appeared
��� stim.setImage(imageName) #set to next image in the list
��� #now work out how much longer to wait, accounting for stim loading
��� tElapsed = clock.getTime()-tStart
��� tRemaining = tStimTime - tElapsed
��� core.wait(tRemaining)
StaticPeriod was added in v1.78 mainly as a way to make the above code easier from within Builder.

best wishes,
Jon
>> > file� so

This message and any attachment are intended solely for the addressee and may contain confidential information. If you have received this message in error, please send it back to me, and immediately delete it.�� Please do not use, copy or disclose the information contained in this message or in any attachment.� Any views or opinions expressed by the author of this email do not necessarily reflect the views of the University of Nottingham.

This message has been checked for viruses but the contents of an attachment may still contain software viruses which could damage your computer system, you are advised to perform your own checks. Email communications with the University of Nottingham may be monitored as permitted by UK legislation.


--
You received this message because you are subscribed to a topic in the Google Groups "psychopy-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/psychopy-users/kw5qRatA9f0/unsubscribe.
To unsubscribe from this group and all its topics, send an email to psychopy-user...@googlegroups.com.
To post to this group, send email to psychop...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/psychopy-users/52CFC712.1080408%40nottingham.ac.uk.

For more options, visit https://groups.google.com/groups/opt_out.
--
You received this message because you are subscribed to the Google Groups "psychopy-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to psychopy-user...@googlegroups.com.
To post to this group, send email to psychop...@googlegroups.com.

For more options, visit https://groups.google.com/groups/opt_out.
-- 
Jonathan Peirce
Nottingham Visual Neuroscience

Dan Dillon

unread,
Jan 13, 2014, 12:58:22 AM1/13/14
to psychop...@googlegroups.com
Hi Jon. That works beautifully, thank you!

Dan


On Sun, Jan 12, 2014 at 6:51 AM, Jonathan Peirce <jon.p...@gmail.com> wrote:
If you're using coder (I assumed you were on Builder) then you can do this manually, without the need for a static period as such. The logic (pseudo code) is like this:

#sometime before first image presentation:
stim.setImage(imageList[0])

for imageName in imageList[1:]: #loop through other images
    stim.draw()
    win.flip()

    tStart = clock.getTime() #the precise moment the stimulus appeared
    stim.setImage(imageName) #set to next image in the list
    #now work out how much longer to wait, accounting for stim loading
    tElapsed = clock.getTime()-tStart

    tRemaining = tStimTime - tElapsed
    core.wait(tRemaining)
>> > file… so

This message and any attachment are intended solely for the addressee and may contain confidential information. If you have received this message in error, please send it back to me, and immediately delete it.   Please do not use, copy or disclose the information contained in this message or in any attachment.  Any views or opinions expressed by the author of this email do not necessarily reflect the views of the University of Nottingham.

This message has been checked for viruses but the contents of an attachment may still contain software viruses which could damage your computer system, you are advised to perform your own checks. Email communications with the University of Nottingham may be monitored as permitted by UK legislation.


--
You received this message because you are subscribed to a topic in the Google Groups "psychopy-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/psychopy-users/kw5qRatA9f0/unsubscribe.
To unsubscribe from this group and all its topics, send an email to psychopy-user...@googlegroups.com.
To post to this group, send email to psychop...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/psychopy-users/52CFC712.1080408%40nottingham.ac.uk.

For more options, visit https://groups.google.com/groups/opt_out.
--
You received this message because you are subscribed to the Google Groups "psychopy-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to psychopy-user...@googlegroups.com.
To post to this group, send email to psychop...@googlegroups.com.

-- Jonathan Peirce Nottingham Visual Neuroscience
http://www.peirce.org.uk/

--
You received this message because you are subscribed to a topic in the Google Groups "psychopy-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/psychopy-users/kw5qRatA9f0/unsubscribe.
To unsubscribe from this group and all its topics, send an email to psychopy-user...@googlegroups.com.
To post to this group, send email to psychop...@googlegroups.com.

S. K. Sidhu

unread,
Apr 10, 2016, 9:06:36 PM4/10/16
to psychopy-users
Hi all,

Would pre-loading an image still be possible if you are using custom codes to generate a stimuli (as opposed to image files)? If so, how?

Thanks in advance.

Yours sincerely,
Shu

Michael MacAskill

unread,
Apr 10, 2016, 9:17:20 PM4/10/16
to psychop...@googlegroups.com

> On 11/04/2016, at 13:06, S. K. Sidhu <shumetha.k...@gmail.com> wrote:
>
> Would pre-loading an image still be possible if you are using custom codes to generate a stimuli (as opposed to image files)?
Yes.

> If so, how?
Give it a go and come back to us with specific questions.

Regards,

Michael


--
Michael R. MacAskill, PhD 66 Stewart St
Research Director, Christchurch 8011
New Zealand Brain Research Institute NEW ZEALAND

Senior Research Fellow, michael....@nzbri.org
Te Whare Wānanga o Otāgo, Otautahi Ph: +64 3 3786 072
University of Otago, Christchurch http://www.nzbri.org/macaskill

Reply all
Reply to author
Forward
0 new messages