# Display a colored image with pixels stored as numpy array

**URL:** <https://discourse.psychopy.org/t/display-a-colored-image-with-pixels-stored-as-numpy-array/4494>\
**Category:** Coding\
**Created:** [May 10, 2018, 8:42am UTC](https://discourse.psychopy.org/t/display-a-colored-image-with-pixels-stored-as-numpy-array/4494 "2018-05-10T08:42:54Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![alanlee](https://avatars.discourse-cdn.com/v4/letter/a/54ee81/32.png) [@alanlee](https://discourse.psychopy.org/u/alanlee)\
**Post date:** [May 10, 2018, 8:42am UTC](https://discourse.psychopy.org/t/display-a-colored-image-with-pixels-stored-as-numpy-array/4494/1 "2018-05-10T08:42:54Z")

</div>

Hello,

I would like to display a colored disc, with every pixel’s RGB values specified in a numpy array. I can generate the image as a 3D numpy array (n rows of pixel x n columns of pixels x 3 colors RGB), but I can’t find a way to display it.

The closest thread I could find is the following, but it’s not exactly what I’d like to have in my stimulus.

> [@Displaying images stored as numpy array](https://discourse.psychopy.org/t/displaying-images-stored-as-numpy-array/2886):
>
> How do I display gray-scale images available as 2D numpy array? I think this was discussed on the old forum, but I can’t find the solution anymore. Thanks.

May I ask if ImageStim or GratingStim can do this? If so , how do I pass the pixel array to the object?

Thanks,  
Alan

---

<div class="post-metadata">

**Author:** ![djmannion](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/djmannion/32/37535_2.png) [@djmannion](https://discourse.psychopy.org/u/djmannion)\
**Post date:** [May 10, 2018, 11:03pm UTC](https://discourse.psychopy.org/t/display-a-colored-image-with-pixels-stored-as-numpy-array/4494/2 "2018-05-10T23:03:26Z")

</div>

You can pass the array as the `image` argument to `ImageStim` or the `tex` argument to `GratingStim`.

---

<div class="post-metadata">

**Author:** ![alanlee](https://avatars.discourse-cdn.com/v4/letter/a/54ee81/32.png) [@alanlee](https://discourse.psychopy.org/u/alanlee)\
**Post date:** [May 11, 2018, 10:41am UTC](https://discourse.psychopy.org/t/display-a-colored-image-with-pixels-stored-as-numpy-array/4494/3 "2018-05-11T10:41:43Z")

</div>

Thanks! It works now with ImageStim. I was stuck before as I didn’t know the size argument is necessary for ImageStim.

---

<div class="post-metadata">

**Author:** ![cahahn](https://avatars.discourse-cdn.com/v4/letter/c/59ef9b/32.png) [@cahahn](https://discourse.psychopy.org/u/cahahn)\
**Post date:** [September 15, 2020, 2:49pm UTC](https://discourse.psychopy.org/t/display-a-colored-image-with-pixels-stored-as-numpy-array/4494/4 "2020-09-15T14:49:58Z")

</div>

Hi,

I’m still stuck on trying to display a 3D numpy array as an image.

Here’s a sample of what I’m trying:

```auto
win = visual.Window(
    size=[2560, 1440], fullscr=True, screen=0, 
    winType='pyglet', allowGUI=False, allowStencil=False,
    monitor='testMonitor', color=[0,0,0], colorSpace='rgb',
    blendMode='avg', useFBO=True, 
    units='height')

stim_img=np.load('image_to_view.npy')
type(stim_img)
#This outputs: <class 'numpy.ndarray'> 
np.shape(stim_img)
#This outputs: (504, 334, 3)

stim = visual.ImageStim(win,image=stim_img)

```

This `stim` variable later gets input into the routine and flipped to the window etc. However, all that appears is a mostly red static image.

The values in the array are the rbg 255 pixel values. So I tried replacing `stim = visual.ImageStim(win,image=stim_img)` with `stim = visual.ImageStim(win,image=stim_img,colorSpace='rgb255')`. This latter version produced a mostly blue static image.

Any help would be greatly appreciated!

EDIT: Updating to include PsychoPy version: 2020.2.4

---

<div class="post-metadata">

**Author:** ![jon](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/jon/32/22_2.png) [@jon](https://discourse.psychopy.org/u/jon)\
**Post date:** [September 17, 2020, 10:57am UTC](https://discourse.psychopy.org/t/display-a-colored-image-with-pixels-stored-as-numpy-array/4494/5 "2020-09-17T10:57:52Z")

</div>

Could you upload the npy file (and a screenshot of what you’re expecting the image to look like)? I expect it’s a scaling issue but hard to know without seeing the array

---

<div class="post-metadata">

**Author:** ![cahahn](https://avatars.discourse-cdn.com/v4/letter/c/59ef9b/32.png) [@cahahn](https://discourse.psychopy.org/u/cahahn)\
**Post date:** [September 17, 2020, 3:08pm UTC](https://discourse.psychopy.org/t/display-a-colored-image-with-pixels-stored-as-numpy-array/4494/6 "2020-09-17T15:08:00Z")

</div>

Thanks so much! Discourse doesn’t support uploading .npy files unfortunately. Instead, here is a very small portion of the image so that you can create the array on your end:

```auto

image_to_view=np.array([[[27, 122, 148],
        [27, 122, 148],
        [27, 122, 148],
        [27, 122, 148],
        [31, 125, 149],
        [30, 123, 149],
        [29, 123, 148],
        [16, 118, 150],
        [35, 125, 145],
        [255, 219, 100]],

       [[27, 122, 148],
        [27, 122, 148],
        [27, 122, 148],
        [25, 121, 147],
        [7, 110, 138],
        [8, 111, 139],
        [28, 124, 148],
        [16, 118, 150],
        [35, 125, 145],
        [255, 219, 100]],

       [[27, 122, 148],
        [27, 122, 148],
        [22, 118, 145],
        [8, 111, 140],
        [84, 155, 175],
        [74, 149, 170],
        [8, 110, 138],
        [15, 116, 148],
        [36, 125, 145],
        [255, 219, 100]],

       [[27, 122, 148],
        [30, 123, 149],
        [1, 106, 136],
        [148, 191, 205],
        [255, 255, 255],
        [255, 255, 255],
        [115, 173, 187],
        [0, 104, 138],
        [39, 128, 148],
        [255, 219, 99]],

       [[27, 122, 148],
        [29, 124, 148],
        [5, 108, 137],
        [239, 245, 247],
        [255, 255, 255],
        [255, 255, 255],
        [207, 226, 230],
        [0, 98, 135],
        [41, 129, 148],
        [255, 219, 99]],

       [[27, 122, 148],
        [30, 125, 149],
        [2, 108, 136],
        [116, 174, 190],
        [255, 255, 255],
        [255, 255, 255],
        [88, 157, 175],
        [0, 106, 141],
        [39, 127, 148],
        [255, 219, 99]],

       [[27, 122, 148],
        [27, 122, 148],
        [24, 120, 146],
        [4, 108, 137],
        [47, 133, 158],
        [40, 129, 153],
        [6, 110, 137],
        [16, 117, 150],
        [36, 125, 145],
        [255, 219, 100]],

       [[27, 122, 148],
        [27, 122, 148],
        [27, 122, 148],
        [29, 123, 149],
        [16, 115, 143],
        [18, 117, 143],
        [31, 124, 149],
        [16, 118, 150],
        [35, 125, 145],
        [255, 219, 100]],

       [[27, 122, 148],
        [27, 122, 148],
        [27, 122, 148],
        [27, 122, 148],
        [30, 123, 149],
        [28, 123, 149],
        [29, 123, 148],
        [16, 118, 150],
        [35, 125, 145],
        [255, 219, 100]],

       [[27, 122, 148],
        [27, 122, 148],
        [27, 122, 148],
        [27, 122, 148],
        [27, 122, 148],
        [27, 122, 148],
        [29, 123, 148],
        [16, 118, 150],
        [35, 125, 145],
        [255, 219, 100]]])

```

Here is the screenshot of what that small part should look like:

 ![Screen Shot 2020-09-17 at 11.06.20 AM](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/3X/a/6/a6002f6737495aed142f0a114e7051185f09689d.png)

Thanks again!

---

<div class="post-metadata">

**Author:** ![cahahn](https://avatars.discourse-cdn.com/v4/letter/c/59ef9b/32.png) [@cahahn](https://discourse.psychopy.org/u/cahahn)\
**Post date:** [October 28, 2020, 3:15pm UTC](https://discourse.psychopy.org/t/display-a-colored-image-with-pixels-stored-as-numpy-array/4494/7 "2020-10-28T15:15:55Z")

</div>

Hi again! I updated psychopy to the most recent release and am still running into issues. You stated that you suspect it is a scaling issue. Can you clarify what you mean by that? Thanks so much!

---

<div class="post-metadata">

**Author:** ![tomwallis](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/tomwallis/32/944_2.png) [@tomwallis](https://discourse.psychopy.org/u/tomwallis)\
**Post date:** [October 1, 2021, 12:02pm UTC](https://discourse.psychopy.org/t/display-a-colored-image-with-pixels-stored-as-numpy-array/4494/8 "2021-10-01T12:02:45Z")

</div>

> [@alanlee](#):
>
> I didn’t know the size argument is necessary for ImageStim.

I’ve been experiencing the same issue as the OP, and wanted to highlight that the solution indeed seems to be that you _must_ pass a `size` to `ImageStim` when using a numpy array as input. You don’t need to pass one if using an image path (e.g. to a `.png`). This is confusing.

I wonder if there could be a [recipie / how-to](https://psychopy.org/recipes/index.html) added for the recommended method to display images (from either image files or from numpy array)?

---

<div class="post-metadata">

**Author:** ![tomwallis](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/tomwallis/32/944_2.png) [@tomwallis](https://discourse.psychopy.org/u/tomwallis)\
**Post date:** [October 3, 2021, 7:43pm UTC](https://discourse.psychopy.org/t/display-a-colored-image-with-pixels-stored-as-numpy-array/4494/9 "2021-10-03T19:43:08Z")

</div>

I started a recipe [here](https://github.com/psychopy/psychopy/pull/4296)
