# Randomly select some image files from the folder as stimuli

**URL:** <https://discourse.psychopy.org/t/randomly-select-some-image-files-from-the-folder-as-stimuli/19336>\
**Category:** Builder\
**Tags:** randomization, screen\
**Created:** [January 3, 2021, 10:48am UTC](https://discourse.psychopy.org/t/randomly-select-some-image-files-from-the-folder-as-stimuli/19336 "2021-01-03T10:48:43Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ayang](https://avatars.discourse-cdn.com/v4/letter/a/848f3c/32.png) [@Ayang](https://discourse.psychopy.org/u/Ayang)\
**Post date:** [January 3, 2021, 10:48am UTC](https://discourse.psychopy.org/t/randomly-select-some-image-files-from-the-folder-as-stimuli/19336/1 "2021-01-03T10:48:43Z")

</div>

**OS** (e.g. Win10): Win10  
**PsychoPy version** (e.g. 1.84.x): v2020.2.10

**What are you trying to achieve?:**  
I hope to randomly select some images from a folder to appear on the screen as stimulation, so I added the following codes with Code Component, hoping to achieve it, but it kept reporting errors. I don’t know what to do, and I hope to get help.Thanks

**What did you try to make it work?:**  
import os  
import random

filenames= os.listdir(’./pics/’)  
random.shuffle(filenames)  
list\_for\_centers=filenames[:10]  
target=[]

for item in list\_for\_centers:  
target\_name=’./pics/’+item  
target.append(target\_name)

print(target)

**What specifically went wrong when you tried that?:**  
14.3494 ERROR Couldn’t make sense of requested image.  
Traceback (most recent call last):  
File “C:\Program Files\PsychoPy3\lib\site-packages\psychopy\visual\basevisual.py”, line 836, in \_createTexture  
im = tex.copy().transpose(Image.FLIP\_TOP\_BOTTOM)  
AttributeError: ‘list’ object has no attribute ‘transpose’

During handling of the above exception, another exception occurred:

Traceback (most recent call last):  
File “C:\Users\31279\pythonProject\exp2\_test\_lastrun.py”, line 353, in   
image.setImage(target)  
File “C:\Program Files\PsychoPy3\lib\site-packages\psychopy\visual\image.py”, line 305, in setImage  
setAttribute(self, ‘image’, value, log)  
File “C:\Program Files\PsychoPy3\lib\site-packages\psychopy\tools\attributetools.py”, line 141, in setAttribute  
setattr(self, attrib, value)  
File “C:\Program Files\PsychoPy3\lib\site-packages\psychopy\tools\attributetools.py”, line 32, in **set**  
newValue = self.func(obj, value)  
File “C:\Program Files\PsychoPy3\lib\site-packages\psychopy\visual\image.py”, line 292, in image  
wrapping=False)  
File “C:\Program Files\PsychoPy3\lib\site-packages\psychopy\visual\basevisual.py”, line 841, in \_createTexture  
raise AttributeError(msg)  
AttributeError: Couldn’t make sense of requested image.  
Exception ignored in: \<bound method TextStim. **del** of \<psychopy.visual.text.TextStim object at 0x0000022A168304E0\>\>  
Traceback (most recent call last):  
File “C:\Program Files\PsychoPy3\lib\site-packages\psychopy\visual\text.py”, line 240, in **del**  
File “C:\Program Files\PsychoPy3\lib\site-packages\pyglet\gl\lib.py”, line 97, in errcheck  
ImportError: sys.meta\_path is None, Python is likely shutting down

##### Experiment ended.

Include pasted full error message if possible. “That didn’t work” is not enough information.

---

<div class="post-metadata">

**Author:** ![tandy](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/tandy/32/9246_2.png) [@tandy](https://discourse.psychopy.org/u/tandy)\
**Post date:** [January 3, 2021, 11:25am UTC](https://discourse.psychopy.org/t/randomly-select-some-image-files-from-the-folder-as-stimuli/19336/2 "2021-01-03T11:25:54Z")

</div>

Hi,

instead of using a complicated code component, you could just use a random loop and an excel file that contains all the images filenames. Then, have a simpler code component that keep tracts of the number of trials and end the loop after a certain number of images are presented (e.g.10) = random subset of images appear on screen.

tandy

---

<div class="post-metadata">

**Author:** ![Ayang](https://avatars.discourse-cdn.com/v4/letter/a/848f3c/32.png) [@Ayang](https://discourse.psychopy.org/u/Ayang)\
**Post date:** [January 4, 2021, 5:18am UTC](https://discourse.psychopy.org/t/randomly-select-some-image-files-from-the-folder-as-stimuli/19336/3 "2021-01-04T05:18:27Z")

</div>

> [@tandy](#):
>
> keep tracts of the number of trials and end the loop after a certain number of images are presented (e.g.10) = random subset of images appear on screen.

Sorry, I don’t quite understand it, could you explain it in more detail? Thanks!!！😀

---

<div class="post-metadata">

**Author:** ![Michael](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/michael/32/16_2.png) [@Michael](https://discourse.psychopy.org/u/Michael)\
**Post date:** [January 4, 2021, 6:14am UTC](https://discourse.psychopy.org/t/randomly-select-some-image-files-from-the-folder-as-stimuli/19336/4 "2021-01-04T06:14:51Z")

</div>

Probably worth following @tandy’s advice. But here is the reason for your error: you are trying to set the image filename of a stimulus to be `target`. That variable is a _ **list** _ of file names, and the image stimulus doesn’t know what to do with a list object. You need to refer to a _specific entry_ in that list in order to provide a valid input.

---

<div class="post-metadata">

**Author:** ![Ayang](https://avatars.discourse-cdn.com/v4/letter/a/848f3c/32.png) [@Ayang](https://discourse.psychopy.org/u/Ayang)\
**Post date:** [January 4, 2021, 6:40am UTC](https://discourse.psychopy.org/t/randomly-select-some-image-files-from-the-folder-as-stimuli/19336/6 "2021-01-04T06:40:49Z")

</div>

🆗! I got it!! Thanks! But actually I don’t know what @tandy means and how to do…😭

---

<div class="post-metadata">

**Author:** ![tandy](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/tandy/32/9246_2.png) [@tandy](https://discourse.psychopy.org/u/tandy)\
**Post date:** [January 4, 2021, 7:42am UTC](https://discourse.psychopy.org/t/randomly-select-some-image-files-from-the-folder-as-stimuli/19336/7 "2021-01-04T07:42:05Z")

</div>

Add a code component that says something like this and place it into your image trial:

In the begin experiment tab:

```auto
myCounter = 0 #set the counter to 0

```

In the begin routine tab

```auto
myCounter = myCounter + 1
if myCounter >= 10:
	currentLoop.finished = True #after 10 images are presented stop the loop

```

tandy

---

<div class="post-metadata">

**Author:** ![Ayang](https://avatars.discourse-cdn.com/v4/letter/a/848f3c/32.png) [@Ayang](https://discourse.psychopy.org/u/Ayang)\
**Post date:** [January 4, 2021, 9:29am UTC](https://discourse.psychopy.org/t/randomly-select-some-image-files-from-the-folder-as-stimuli/19336/8 "2021-01-04T09:29:16Z")

</div>

Thank you soooo much!!! 😭It works!! You saved my life 😭

But there is a new problem now…I want the same images to appear twice, so I set “nReps$=2” in the loop properties. If loop finished, images can’t repeated anymore. What should I do?

Thank you again and again 😀

---

<div class="post-metadata">

**Author:** ![tandy](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/tandy/32/9246_2.png) [@tandy](https://discourse.psychopy.org/u/tandy)\
**Post date:** [January 4, 2021, 11:34am UTC](https://discourse.psychopy.org/t/randomly-select-some-image-files-from-the-folder-as-stimuli/19336/9 "2021-01-04T11:34:31Z")

</div>

Ok so try this:

instead of having nReps$=2 in the trial loop, leave the repetitions as 1. Then create a second loop above the first one, set to sequential and with no condition file, set to repeat 2 times. Should work,

tandy

---

<div class="post-metadata">

**Author:** ![Ayang](https://avatars.discourse-cdn.com/v4/letter/a/848f3c/32.png) [@Ayang](https://discourse.psychopy.org/u/Ayang)\
**Post date:** [January 4, 2021, 12:41pm UTC](https://discourse.psychopy.org/t/randomly-select-some-image-files-from-the-folder-as-stimuli/19336/10 "2021-01-04T12:41:45Z")

</div>

Is that what you mean? It seems doesn’t work… 😭

 ![image](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/3X/f/2/f29672ca8f23bcb2f65ef3c0690a9aa23b980823.png)

---

<div class="post-metadata">

**Author:** ![tandy](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/tandy/32/9246_2.png) [@tandy](https://discourse.psychopy.org/u/tandy)\
**Post date:** [January 4, 2021, 1:04pm UTC](https://discourse.psychopy.org/t/randomly-select-some-image-files-from-the-folder-as-stimuli/19336/11 "2021-01-04T13:04:53Z")

</div>

What is not working?

Less elegant solution: just add 3 new routines (fixation - faces\_alone, alone\_rating) in the flow with a loop around them (so it’s 3 routine inside loop1, 3 routine inside loop2, each loop ends after 10 images).

tandy

---

<div class="post-metadata">

**Author:** ![Ayang](https://avatars.discourse-cdn.com/v4/letter/a/848f3c/32.png) [@Ayang](https://discourse.psychopy.org/u/Ayang)\
**Post date:** [January 4, 2021, 1:33pm UTC](https://discourse.psychopy.org/t/randomly-select-some-image-files-from-the-folder-as-stimuli/19336/12 "2021-01-04T13:33:17Z")

</div>

It won’t repeat 2 times…

Maybe go back to the code component, I wrote new codes, but only showing 1 picture that loop will finish…How does it present all 10 pictures？

```auto
import os
import random

filenames= os.listdir('./pics/')
random.shuffle(filenames)
list_for_centers=filenames[:10]

for item in list_for_centers:
    target='./pics/'+item
    print(target)

```

---

<div class="post-metadata">

**Author:** ![tandy](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/tandy/32/9246_2.png) [@tandy](https://discourse.psychopy.org/u/tandy)\
**Post date:** [January 4, 2021, 2:06pm UTC](https://discourse.psychopy.org/t/randomly-select-some-image-files-from-the-folder-as-stimuli/19336/13 "2021-01-04T14:06:26Z")

</div>

Ok I think I did not understood what you wanted to do. So correct me if I’m wrong: you want to show 10 random images twice taken from a larger sample. So for example out of 100 images you want to show 10 at random (e.g. image 1, 56, 67, 44, 9, 34, 55, 90, 39, 34) and then (after a break/pause?) you want to present them again (1, 56, 67, 44, 9, 34, 55, 90, 39, 34) in the same or different order?

tandy

---

<div class="post-metadata">

**Author:** ![Michael](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/michael/32/16_2.png) [@Michael](https://discourse.psychopy.org/u/Michael)\
**Post date:** [January 4, 2021, 8:18pm UTC](https://discourse.psychopy.org/t/randomly-select-some-image-files-from-the-folder-as-stimuli/19336/14 "2021-01-04T20:18:31Z")

</div>

The reason the second loop won’t run is because this line:

```
myCounter = 0 #set the counter to 0

```

happens only once, at the beginning of the experiment. So on the second loop, the counter will still be `10` and hence the loop will finish immediately. So delete that line and instead put this in the “begin routine” tab:

```auto
if trials.thisN == 0:
    myCounter = 0 # set the counter to 0

```

so it gets reset at the start of each loop.

---

<div class="post-metadata">

**Author:** ![Ayang](https://avatars.discourse-cdn.com/v4/letter/a/848f3c/32.png) [@Ayang](https://discourse.psychopy.org/u/Ayang)\
**Post date:** [January 5, 2021, 2:07am UTC](https://discourse.psychopy.org/t/randomly-select-some-image-files-from-the-folder-as-stimuli/19336/15 "2021-01-05T02:07:18Z")

</div>

Yeah you are right. 😃 But I don’t need a break.  
I want to show 10 pictures at random (e.g. image 1, 56, 67, 44, 9, 34, 55, 90, 39, 34) and then present them again immediately (1, 56, 67, 44, 9, 34, 55, 90, 39, 34) in a different order. And this is the first block.

The second block is: picking 1 pictures (e.g. image 55) from the 10 (1, 56, 67, 44, 9, 34, 55, 90, 39, 34) as the target stimulus，then picking 4 pictures randomly (e.g. image 56, 9, 90, 44) from the remainder 9 (1, 56, 67, 44, 9, 34, 90, 39, 34) around the target. It still needs to present them again immediately.

I don’t know whether I spelt out? 😥 Thanks

---

<div class="post-metadata">

**Author:** ![hakime\_ghaffari](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/hakime_ghaffari/32/14309_2.png) [@hakime\_ghaffari](https://discourse.psychopy.org/u/hakime_ghaffari)\
**Post date:** [February 17, 2021, 3:59pm UTC](https://discourse.psychopy.org/t/randomly-select-some-image-files-from-the-folder-as-stimuli/19336/17 "2021-02-17T15:59:51Z")

</div>

Hey Michael,  
Would you explain how to do that in coder?

---

<div class="post-metadata">

**Author:** ![Michael](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/michael/32/16_2.png) [@Michael](https://discourse.psychopy.org/u/Michael)\
**Post date:** [February 17, 2021, 11:08pm UTC](https://discourse.psychopy.org/t/randomly-select-some-image-files-from-the-folder-as-stimuli/19336/18 "2021-02-17T23:08:36Z")

</div>

> [@hakime\_ghaffari](#):
>
> Would you explain how to do that in coder?

Hi, from such a long thread, I’m not sure what the specific question is here. Could you provide some more detail (probably best to create a new topic for it)?

---

<div class="post-metadata">

**Author:** ![hakime\_ghaffari](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/hakime_ghaffari/32/14309_2.png) [@hakime\_ghaffari](https://discourse.psychopy.org/u/hakime_ghaffari)\
**Post date:** [February 18, 2021, 3:17pm UTC](https://discourse.psychopy.org/t/randomly-select-some-image-files-from-the-folder-as-stimuli/19336/19 "2021-02-18T15:17:13Z")

</div>

sure, thank you.
