# Trouble loading image

**URL:** <https://discourse.processing.org/t/trouble-loading-image/31915>\
**Category:** Processing.py\
**Created:** [August 25, 2021, 8:08am UTC](https://discourse.processing.org/t/trouble-loading-image/31915 "2021-08-25T08:08:21Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![LindowMan](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/lindowman/32/14031_2.png) [@LindowMan](https://discourse.processing.org/u/LindowMan)\
**Post date:** [August 25, 2021, 8:08am UTC](https://discourse.processing.org/t/trouble-loading-image/31915/1 "2021-08-25T08:08:21Z")

</div>

Really can’t see what the problem is supposed to be here.

```auto
def setup():
    img = loadImage("b2.jpg")
    size(img.width, img.height)

```

Result is the following error:  
processing.app.SketchException: NameError: global name ‘img’ is not defined

how so? img is declared right there in the preceding line.  
I have tried declaring it as a global variable both inside and outside of setup(). Didn’t help.

Thanks a lot

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [August 25, 2021, 9:03am UTC](https://discourse.processing.org/t/trouble-loading-image/31915/3 "2021-08-25T09:03:44Z")

</div>

Size should be before loadImage

Try size() with a fixed size like 600,600

Then check the file. Is the file name really correct. It’s case-sensitive. It should be in the data folder (also case sensitive)

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [August 25, 2021, 9:50am UTC](https://discourse.processing.org/t/trouble-loading-image/31915/4 "2021-08-25T09:50:35Z")

</div>

> **[Reference](https://processing.org/reference/settings_.html)**
>
> The settings() function is new with Processing 3.0. It's not needed in most sketches. It's only useful when it's absolutely necessary to define the parameters to size() with a variable. …

```auto
"""
 * LoadImage in Settings (v1.0.0)
 * GoToLoop (2019/Aug/25)
 * https://Discourse.Processing.org/t/trouble-loading-image/31915/4
"""

URL = "https://"\
      "upload.Wikimedia.org/wikipedia/commons/thumb/5/59/"\
      "Processing_Logo_Clipped.svg/480px-Processing_Logo_Clipped.svg.png"

print URL

def settings():
    global img
    img = loadImage(URL)
    print img.width, img.height

    size(img.width, img.height)
    noLoop()

def draw(): background(img)

```

---

<div class="post-metadata">

**Author:** ![tabreturn](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tabreturn/32/3697_2.png) [@tabreturn](https://discourse.processing.org/u/tabreturn)\
**Post date:** [August 25, 2021, 9:53am UTC](https://discourse.processing.org/t/trouble-loading-image/31915/5 "2021-08-25T09:53:55Z")

</div>

As per @GoToLoop’s suggestion – the simple solution is:

```auto
def settings():
    img = loadImage('b2.jpg')
    size(img.width, img.height)

```

---

<div class="post-metadata">

**Author:** ![LindowMan](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/lindowman/32/14031_2.png) [@LindowMan](https://discourse.processing.org/u/LindowMan)\
**Post date:** [August 30, 2021, 12:47pm UTC](https://discourse.processing.org/t/trouble-loading-image/31915/6 "2021-08-30T12:47:49Z")

</div>

I did try that, too. filename is correct, files are in the folder, too. I have few other files in there and tried them, all, worked for none of them. Hardcoding size() works for me:

```auto
def setup():
    global img
    size(600,600)
    img = loadImage("b2.jpg")

```

But I’d like this to be dynamic and want to set it by the size of the actual image.

---

<div class="post-metadata">

**Author:** ![LindowMan](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/lindowman/32/14031_2.png) [@LindowMan](https://discourse.processing.org/u/LindowMan)\
**Post date:** [August 30, 2021, 12:57pm UTC](https://discourse.processing.org/t/trouble-loading-image/31915/8 "2021-08-30T12:57:39Z")

</div>

Thanks but as I replied to GoToLoop, this gives me a NullPointerException

---

<div class="post-metadata">

**Author:** ![LindowMan](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/lindowman/32/14031_2.png) [@LindowMan](https://discourse.processing.org/u/LindowMan)\
**Post date:** [August 30, 2021, 12:59pm UTC](https://discourse.processing.org/t/trouble-loading-image/31915/9 "2021-08-30T12:59:56Z")

</div>

Thanks, but strangely enough, using settings() gives me a NullPointerException, both with fixed size as well as when setting size() dynamically. It works only with setup(). Extra weird because I got it to work exactly as you point out in an earlier sketch I made. I have some further functions in the code.  
Actually I am a bit confused about Python Mode. Usually I put all my functions at the top when coding in Python. Does Processing actually run Python, or does it translate a Python Mode sketch to Java and then runs it?

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [August 30, 2021, 9:50pm UTC](https://discourse.processing.org/t/trouble-loading-image/31915/10 "2021-08-30T21:50:00Z")

</div>

> [@LindowMan](#):
>
> , using settings() gives me a NullPointerException,

I’ve re-tested my posted code again and it works in my Processing v3.5.4 w/ Python Mode v.3063.

Make sure you’re not using any alpha or beta versions.

> [@LindowMan](#):
>
> Does Processing actually run Python, or does it translate a Python Mode sketch to Java and then runs it?

Processing is written in Java and Python Mode uses Jython:  
[Processing.GitHub.io/processing-javadocs/core/](http://Processing.GitHub.io/processing-javadocs/core/)

> **[Home](https://www.jython.org/)**
>
> The Python runtime on the JVM

---

<div class="post-metadata">

**Author:** ![LindowMan](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/lindowman/32/14031_2.png) [@LindowMan](https://discourse.processing.org/u/LindowMan)\
**Post date:** [August 31, 2021, 3:43pm UTC](https://discourse.processing.org/t/trouble-loading-image/31915/11 "2021-08-31T15:43:54Z")

</div>

> [@GoToLoop](#):
>
> Processing v3.5.4 w/ Python Mode v.3063.

Same here. This is really strange… It used to work just 2 weeks ago and I haven’t installed or changed anything since then

---

<div class="post-metadata">

**Author:** ![LindowMan](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/lindowman/32/14031_2.png) [@LindowMan](https://discourse.processing.org/u/LindowMan)\
**Post date:** [August 31, 2021, 4:04pm UTC](https://discourse.processing.org/t/trouble-loading-image/31915/12 "2021-08-31T16:04:11Z")

</div>

It works again now!  
Turns out is was my Java installation that was out of date… One Java update later and all works fine again.  
thanks for tipping me in that “check versions” direction GoToLoop!
