Trouble loading image

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

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

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)

"""
 * 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)
1 Like

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

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

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:

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.

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

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?

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.

Processing is written in Java and Python Mode uses Jython:
Processing.GitHub.io/processing-javadocs/core/

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

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!