# Problem with image file for pavlovia

**URL:** <https://discourse.psychopy.org/t/problem-with-image-file-for-pavlovia/7843>\
**Category:** Online experiments\
**Created:** [May 20, 2019, 3:24pm UTC](https://discourse.psychopy.org/t/problem-with-image-file-for-pavlovia/7843 "2019-05-20T15:24:55Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![fffrost](https://avatars.discourse-cdn.com/v4/letter/f/f1d935/32.png) [@fffrost](https://discourse.psychopy.org/u/fffrost)\
**Post date:** [May 20, 2019, 3:24pm UTC](https://discourse.psychopy.org/t/problem-with-image-file-for-pavlovia/7843/1 "2019-05-20T15:24:55Z")

</div>

I uploaded an experiment that I have in PsychoPy onto pavlovia last night ([link](https://pavlovia.org/explore?sort=DEFAULT&search=memant)), and everything seems ok until I try to run it and I get the following message. Is there an issue presenting image files?

**Message:**  
Unfortunately we encountered an error:

- when setting the image of ImageStim: ANT\_instr\_img
- when getting the value of resource: C:\Users\L\Documents\PsychoPy\MemANT\ANT\_instructions.png
- **unknown resource**

Try to run the experiment again. If the error persists, contact the experimenter.

---

<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:** [May 21, 2019, 8:18am UTC](https://discourse.psychopy.org/t/problem-with-image-file-for-pavlovia/7843/2 "2019-05-21T08:18:05Z")

</div>

Could you share a link to the actual experiment rather than a search address? Thanks

---

<div class="post-metadata">

**Author:** ![fffrost](https://avatars.discourse-cdn.com/v4/letter/f/f1d935/32.png) [@fffrost](https://discourse.psychopy.org/u/fffrost)\
**Post date:** [May 21, 2019, 8:37am UTC](https://discourse.psychopy.org/t/problem-with-image-file-for-pavlovia/7843/3 "2019-05-21T08:37:00Z")

</div>

[link](https://pavlovia.org/run/Dunne/memant/html/)

---

<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:** [May 21, 2019, 9:11am UTC](https://discourse.psychopy.org/t/problem-with-image-file-for-pavlovia/7843/4 "2019-05-21T09:11:46Z")

</div>

You’ve set your image file to have the path:  
`C:\Users\L\Documents\PsychoPy\MemANT\ANT_instructions.png`  
That doesn’t exist on the web (or indeed if you move your experiment to a different folder on the local computer).

Always set your images to have a path relative to the experiment (in this case just the image path should just be `ANT_instructions.png`)

---

<div class="post-metadata">

**Author:** ![fffrost](https://avatars.discourse-cdn.com/v4/letter/f/f1d935/32.png) [@fffrost](https://discourse.psychopy.org/u/fffrost)\
**Post date:** [May 21, 2019, 9:18am UTC](https://discourse.psychopy.org/t/problem-with-image-file-for-pavlovia/7843/5 "2019-05-21T09:18:05Z")

</div>

I see! I assumed that because the file exists in the resources folder that this was enough. Thanks for the help

---

<div class="post-metadata">

**Author:** ![Shardul\_Shankar](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/shardul_shankar/32/13209_2.png) [@Shardul\_Shankar](https://discourse.psychopy.org/u/Shardul_Shankar)\
**Post date:** [August 13, 2019, 7:13am UTC](https://discourse.psychopy.org/t/problem-with-image-file-for-pavlovia/7843/6 "2019-08-13T07:13:58Z")

</div>

Hi @fffrost,

Have you been able to fix this problem? Would be able to help me how by explaining the process you did? I am facing similar problem that needs fixing.

Thanks in advance.

---

<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:** [August 13, 2019, 10:04am UTC](https://discourse.psychopy.org/t/problem-with-image-file-for-pavlovia/7843/7 "2019-08-13T10:04:29Z")

</div>

Hi @Shardul_Shankar, the solution is in the post above:

> [@Problem with image file for pavlovia](https://discourse.psychopy.org/t/problem-with-image-file-for-pavlovia/7843/4):
>
> You’ve set your image file to have the path: C:\Users\L\Documents\PsychoPy\MemANT\ANT\_instructions.png That doesn’t exist on the web (or indeed if you move your experiment to a different folder on the local computer). Always set your images to have a path relative to the experiment (in this case just the image path should just be ANT\_instructions.png)

If that doesn’t work for you, could you start a new topic and post the exact details of your particular issue (including any error messages)?

---

<div class="post-metadata">

**Author:** ![Shardul\_Shankar](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/shardul_shankar/32/13209_2.png) [@Shardul\_Shankar](https://discourse.psychopy.org/u/Shardul_Shankar)\
**Post date:** [August 20, 2019, 7:30am UTC](https://discourse.psychopy.org/t/problem-with-image-file-for-pavlovia/7843/8 "2019-08-20T07:30:19Z")

</div>

Hi @Michael,

I have already created a similar topic which discusses my complete problem in detail here:

> [@HTML folder not getting created for online experiment](https://discourse.psychopy.org/t/html-folder-not-getting-created-for-online-experiment/7929/7):
>
> Thank you for replying @jon My experiment is created in six blocks, which uses stimuli conditions file, selecting the row stimulus at random in three types of emotions, i.e. positive, negative and neutral emotions, with each emotion being depicted from another file, which contains the location of the images displaying emotions, which is looped as trials, and the images are stored in a folder inside the location of the experiment. Attached are two of the stimulus and location xlsx file. I did …

This might help you in understanding my problem better. Let me know if you need any further information.

Thanks in advance,  
Shardul

---

<div class="post-metadata">

**Author:** ![PsyTechMMU](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/psytechmmu/32/35713_2.png) [@PsyTechMMU](https://discourse.psychopy.org/u/PsyTechMMU)\
**Post date:** [April 8, 2020, 12:40pm UTC](https://discourse.psychopy.org/t/problem-with-image-file-for-pavlovia/7843/9 "2020-04-08T12:40:05Z")

</div>

> [@jon](#):
>
> Always set your images to have a path relative to the experiment (in this case just the image path should just be `ANT_instructions.png` )

This is how I always work with standalone PsychoPy experiments, but there must be something I’m missing for pavlovia implementations because I’m getting the same error as the OP.

Using a relative path in the .psyexp (e.g., stimuli/image.png) and exporting to html, the stimuli end up inside a resources directory, but the .js maintains the stimuli/image.png path and altering that to resources/stimuli/image.png doesn’t work either. What am I missing and/or doing horrifically wrong here?

---

<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:** [April 8, 2020, 1:20pm UTC](https://discourse.psychopy.org/t/problem-with-image-file-for-pavlovia/7843/10 "2020-04-08T13:20:41Z")

</div>

hmm, not sure. This is the normal structure:

```auto
index.html
myStudy.js
resources/
    conditions.xlsx
    stimuli/
       stim1.png
       stim2.png

```

and in that arrangement, yes, the path stimuli/stim1.png should work fine. What’s the URL for the experiment on [gitlab.pavlovia.org](http://gitlab.pavlovia.org)?

---

<div class="post-metadata">

**Author:** ![PsyTechMMU](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/psytechmmu/32/35713_2.png) [@PsyTechMMU](https://discourse.psychopy.org/u/PsyTechMMU)\
**Post date:** [April 9, 2020, 3:57pm UTC](https://discourse.psychopy.org/t/problem-with-image-file-for-pavlovia/7843/11 "2020-04-09T15:57:05Z")

</div>

Thanks for getting back to me about this, Jon. That structure looks familiar and similar, but not identical: my conditions.xlsx file is not in the /stimuli directory, just in the /resources one. Could that be the issue?

This is one of the projects I’m having this issue with: [https://gitlab.pavlovia.org/PsyTechMMU/pavloviatest](https://gitlab.pavlovia.org/PsyTechMMU/pavloviatest)  
(if that’s the link you meant - apologies if not: despite the way many would describe me, I’m quite new to the world of git)

---

<div class="post-metadata">

**Author:** ![wakecarter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/wakecarter/32/5753_2.png) [@wakecarter](https://discourse.psychopy.org/u/wakecarter)\
**Post date:** [April 9, 2020, 4:18pm UTC](https://discourse.psychopy.org/t/problem-with-image-file-for-pavlovia/7843/12 "2020-04-09T16:18:32Z")

</div>

My single image file in Ebbinghaus Darts is giving an unknown resource error in the local debug mode, but not via Pavlovi. The file is in the experiment root folder plus html/resources. It isn’t referenced in the code snippet, just in a builder component.

---

<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:** [April 10, 2020, 3:06pm UTC](https://discourse.psychopy.org/t/problem-with-image-file-for-pavlovia/7843/13 "2020-04-10T15:06:28Z")

</div>

Gary, sorry, yes the conditions file outside the folder (I’ve edited my post to reflect that).

I just copied yours with no changes and the image loading worked fine, so not sure why yours went wrong. sometimes though it’s a cache thing - if you updated the experiment (initially failed to include the image file) and then try to run, the javascript/html/resources from the first attempt get cached by your browser and then your changes aren’t reflected even though they have made it to the server! Try a Shift-R to reload and fully refresh the cache on the browser.  
BUT although I get the images to load fine I got stuck on your liberal use of random and randint. random() need to be converted to use Math.random in the JS code and that autotranslation clearly isn’t working. Similarly randint needs to be converted to a function that we should define at the top of the script but that currenlty has to be added separately (I think we’ll be doing this one automatically in next release). You can add the necessary code like this:

Add a code component to one of your initial screens (eg. instructs) and create a code component that’s pure JS (not auto-trans). In the Begin Experiment add this code:

```auto
// alias for random func
random = Math.random

// also define randint
function randint(min, max) {
  return Math.floor(Math.random() * (max - min) ) + min;
}

```

and then your randint (and random) functions will work again 🙂

---

<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:** [April 10, 2020, 3:07pm UTC](https://discourse.psychopy.org/t/problem-with-image-file-for-pavlovia/7843/14 "2020-04-10T15:07:28Z")

</div>

Wakefield I don’t know the cause of your issue I’m afraid. I think the local debug mode needs more debugging! 😕

---

<div class="post-metadata">

**Author:** ![PsyTechMMU](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/psytechmmu/32/35713_2.png) [@PsyTechMMU](https://discourse.psychopy.org/u/PsyTechMMU)\
**Post date:** [April 17, 2020, 10:14am UTC](https://discourse.psychopy.org/t/problem-with-image-file-for-pavlovia/7843/15 "2020-04-17T10:14:58Z")

</div>

Apologies for the delayed response, but thank you so much for all this assistance. The random stuff (so “liberal” just because the experiment is intended as just a demo) was something I’d been grappling with and was in the process of trying to implement as Math.random JS, so your confirmation that this is what I need to do (and the functions!) are greatly appreciated. Thank you!

Unfortunately, however, after adding the function code you provided (in the Begin Experiment tab of a pure JS, not auto-JS Code component right at the beginning of the experiment) I now get:

 ![image](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/3X/e/1/e1ee82251da60223b01825c93a56808561a302e4.png)  
Something to do with me feeding it negative and positive values perhaps?

> [@jon](#):
>
> Wakefield I don’t know the cause of your issue I’m afraid. I think the local debug mode needs more debugging! 😕

I get the same for images when using the local debug mode.

---

<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:** [April 17, 2020, 10:24am UTC](https://discourse.psychopy.org/t/problem-with-image-file-for-pavlovia/7843/16 "2020-04-17T10:24:08Z")

</div>

> [@PsyTechMMU](#):
>
> ![image](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/3X/e/1/e1ee82251da60223b01825c93a56808561a302e4.png)
> 
> Something to do with me feeding it negative and positive values perhaps?

No, this looks like a failure to define the function with the correct scope (e.g. it is being defined only for the BeginRoutine section of code rather than the whole script. ☹ Less efficient, you could add it to the code where it’s needed instead (was it on Begin Routine?)

In the next release I’ll make sure these functions are defined at the top of the script so you won’t need to define them at all

---

<div class="post-metadata">

**Author:** ![PsyTechMMU](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/psytechmmu/32/35713_2.png) [@PsyTechMMU](https://discourse.psychopy.org/u/PsyTechMMU)\
**Post date:** [April 17, 2020, 11:10am UTC](https://discourse.psychopy.org/t/problem-with-image-file-for-pavlovia/7843/17 "2020-04-17T11:10:50Z")

</div>

> [@jon](#):
>
> No, this looks like a failure to define the function with the correct scope (e.g. it is being defined only for the BeginRoutine section of code rather than the whole script. ☹ Less efficient, you could add it to the code where it’s needed instead (was it on Begin Routine?)

Ah, right. Yes, that seems to be it and it appears to work now. Thanks again! 😄

Alas, I now get as far as the yesno routine, which is set to respond to y or n keys and end the loop accordingly, but I get an error about length. It looks (to me) like the auto-JS code isn’t correct for this and maybe ynResp.keys.length should be Object.keys(ynResp).length:

```auto
if (((ynResp.keys !== null) && (ynResp.keys.length > 0))) {
    flashingArrows.finished = 1;
}

```

Or, rather, I’ve not used the best Python code for it):

```auto
if ynResp.keys is not None and len(ynResp.keys)>0 :
        flashingArrows.finished=1

```

Any ideas?

> [@jon](#):
>
> In the next release I’ll make sure these functions are defined at the top of the script so you won’t need to define them at all

Excellent news! 😊

---

<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:** [April 20, 2020, 4:24pm UTC](https://discourse.psychopy.org/t/problem-with-image-file-for-pavlovia/7843/18 "2020-04-20T16:24:26Z")

</div>

Someone suggested that the issue is that the `yResp.keys` is initialised to undefined in the JS code, not `null`.  
For the Code Component where this isn’t working, could you switch from Auto to Both so that you can manually set the code and set it to:

```auto
if (((ynResp.keys !== undefined) && (ynResp.keys.length > 0))) {
    flashingArrows.finished = 1;
}

```

If that works we can add it to the auto-convert tweaks we need to make 🙂

---

<div class="post-metadata">

**Author:** ![PsyTechMMU](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/psytechmmu/32/35713_2.png) [@PsyTechMMU](https://discourse.psychopy.org/u/PsyTechMMU)\
**Post date:** [April 21, 2020, 1:23pm UTC](https://discourse.psychopy.org/t/problem-with-image-file-for-pavlovia/7843/19 "2020-04-21T13:23:32Z")

</div>

Thanks again, Jon. Happily, it no longer errors out, but it also doesn’t work either… What I mean is that pressing keys (specifically, y or n) doesn’t exit the loop, the loop just plays out (via Pavlovia, it works fine as it did via PsychoPy).

Sidenote: is it expected for the Code component to automatically revert to Auto after being set to Both? As instructed, I set it to Both before amending the JS code, but when going back to it, it’s back to Auto…

---

<div class="post-metadata">

**Author:** ![dvbridges](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/dvbridges/32/1918_2.png) [@dvbridges](https://discourse.psychopy.org/u/dvbridges)\
**Post date:** [April 21, 2020, 1:36pm UTC](https://discourse.psychopy.org/t/problem-with-image-file-for-pavlovia/7843/20 "2020-04-21T13:36:24Z")

</div>

@PsyTechMMU, to end the loop, you have to use `trials.finished = true;`, rather than the name of the actual loop - see [past GitHub issue](https://github.com/psychopy/psychojs/issues/68)

[Next page](https://discourse.psychopy.org/t/problem-with-image-file-for-pavlovia/7843.md?page=2)
