Sizing the display window to fit image dimensions

I’d like to load an image, then size() the display window to fit the actual width and height of the image, using a technique that is considered acceptable according to Processing.py standards. But I have only been able to accomplish the task by inadvisably placing a call to the loadImage() function inside a settings() function, then passing the dimensions of the image to the size() function within the setup() function.

The above may be problematic for a couple of reasons. For one thing, I cannot find information on the settings() function within the Processing.py documentation. In addition, the documentation on settings() for Java Mode specifically advises against the technique described above, stating, in part:

The settings() method runs before the sketch has been set up, so other Processing functions cannot be used at that point. For instance, do not use loadImage() inside settings().

The code is below, and it does seem to work. But is there a more acceptable and better means of doing this that is less likely to break in the future?

def settings():
    global img
    """
    image from:
    https://py.processing.org/tutorials/pixels/imgs/tint1.jpg
    (renamed sunflower.jpg)
    """
    # Load the image; Is this allowed here?
    img = loadImage("sunflower.jpg")
    size(img.width, img.height)

def setup():
    image(img, 0, 0)

def draw():
    x, y = mouseX, mouseY
    pix = get(mouseX, mouseY)
    print("RGB at {}, {}: {}, {}, {}".format(x, y, int(red(pix)), int(green(pix)), int(blue(pix))))

EDIT (December 30, 2020):

Thanks, all, for the help. :smile:

Following is revision of the draw() function, based on Reference: >> (right shift):

def draw():
    x, y = mouseX, mouseY
    argb = get(mouseX, mouseY)
    
    a = (argb >> 24) & 0xFF  # Faster way of getting alpha(argb)
    r = (argb >> 16) & 0xFF  # Faster way of getting red(argb)
    g = (argb >> 8) & 0xFF   # Faster way of getting green(argb)
    b = argb & 0xFF          # Faster way of getting blue(argb)
    
    print("ARGB at {}, {}: {} {}, {}, {}".format(x, y, a, r, g, b))
1 Like

This is a great question. I’m doing the same as you’ve shown and I think we should be OK. The alternative would be something like this, I suppose:

def setup():
  size(400, 400)
  this.surface.setResizable(True)

def draw() :
  background(255);
  line(100, 100, width-100, height-100)

def keyPressed():
  this.surface.setSize(int(random(200, 500)),
                       int(random(200, 500))
                       )

Reference: https://github.com/processing/processing/wiki/Changes-in-3.0

4 Likes

Thanks, @villares, for the reassurance and for the officially sanctioned alternative.

From the link that you posted:

… using variables instead of numbers will cause problems since 2006, and by at least 2009, simply “don’t do it”, but now the chickens have come home to roost.

For now, I’ll leave it as is, as it has a clean look about it, but upon arrival of any roosting chickens, will dash to my keyboard to heed the warnings. :wink:

2 Likes

what does this. mean? I have seen it in p5js but nowhere else. What does it do?

2 Likes

System variable this holds the PApplet’s reference of the whole sketch.

We can access anything from the link below via variable this:

2 Likes

As @GoToLoop has answered better than I could, this is a variable provided by Processing Python mode that holds the PApplet’s reference. It is used often for compatibility with Processing libraries that would require such reference to be set up.

2 Likes

You see it in methods written JavaScript and Java to refer to the current instance of an object. In Python, self. is used in a similar fashion in user-defined classes, by convention, but you could use another name, including this., if you want, provided that you establish it as the first parameter in the method header.

1 Like

so in processing java mode this. is changed to surface.?

In the context of example that @villares provided, this refers to the entire current sketch. That sketch has a property named surface that has a method named setResizable. When True is passed to that method, that method makes the surface of the current sketch capable of being resized.

With dot notation, as we proceed from left to right, each entity to the right of a dot is a method or property of the entity to the left of the dot. Accordingly, surface is a property of what this represents, namely the sketch.

Edit (December 29, 2020):

Run this little script in Python Mode to see some technical details:

def setup():
  print(type(this)) # display the type of this --> <type 'jycessing.PAppletJythonDriver'>
  print(dir(this)) # display a list of valid attributes for this --> ouput includes 'surface'
  print(type(this.surface)) # display the type of this.surface --> <type 'processing.awt.PSurfaceAWT'>
  print(dir(this.surface)) # display a list of valid attributes for this.surface --> output includes 'setResizable'

The comments within the script provide some selected details about the lengthy output. For some examples of defining and using objects in Python Mode, which might provide additional insight into what is going on with the dot notation, here …

this.surface.setSize(int(random(200, 500)),
                       int(random(200, 500))
                       )

… see the Objects submenu of the File menu in Python Mode, especially Objects, and then CompositeObjects.

Screen Shot 2020-12-29 at 8.22.49 PM

2 Likes