# Sizing the display window to fit image dimensions

**URL:** <https://discourse.processing.org/t/sizing-the-display-window-to-fit-image-dimensions/26598>\
**Category:** Processing.py\
**Created:** [December 29, 2020, 9:49pm UTC](https://discourse.processing.org/t/sizing-the-display-window-to-fit-image-dimensions/26598 "2020-12-29T21:49:29Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![javagar](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/javagar/32/11434_2.png) [@javagar](https://discourse.processing.org/u/javagar)\
**Post date:** [December 29, 2020, 9:49pm UTC](https://discourse.processing.org/t/sizing-the-display-window-to-fit-image-dimensions/26598/1 "2020-12-29T21:49:29Z")

</div>

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()`](https://processing.org/reference/settings_.html) 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?

```auto
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. 😄

Following is revision of the `draw()` function, based on [Reference: \>\> (right shift)](https://py.processing.org/reference/rightshift.html):

```auto
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))

```

---

<div class="post-metadata">

**Author:** ![villares](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/villares/32/3166_2.png) [@villares](https://discourse.processing.org/u/villares)\
**Post date:** [December 29, 2020, 9:56pm UTC](https://discourse.processing.org/t/sizing-the-display-window-to-fit-image-dimensions/26598/2 "2020-12-29T21:56:33Z")

</div>

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:

```auto
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](https://github.com/processing/processing/wiki/Changes-in-3.0)

---

<div class="post-metadata">

**Author:** ![javagar](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/javagar/32/11434_2.png) [@javagar](https://discourse.processing.org/u/javagar)\
**Post date:** [December 29, 2020, 10:08pm UTC](https://discourse.processing.org/t/sizing-the-display-window-to-fit-image-dimensions/26598/3 "2020-12-29T22:08:17Z")

</div>

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](https://web.archive.org/web/20060108200811/http://processing.org/reference/size_.html), 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. 😉

---

<div class="post-metadata">

**Author:** ![CodeMasterX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemasterx/32/2942_2.png) [@CodeMasterX](https://discourse.processing.org/u/CodeMasterX)\
**Post date:** [December 29, 2020, 10:20pm UTC](https://discourse.processing.org/t/sizing-the-display-window-to-fit-image-dimensions/26598/4 "2020-12-29T22:20:22Z")

</div>

> [@villares](#):
>
> ` this.surface.setSize`

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

---

<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:** [December 29, 2020, 10:38pm UTC](https://discourse.processing.org/t/sizing-the-display-window-to-fit-image-dimensions/26598/5 "2020-12-29T22:38:45Z")

</div>

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

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

> **[PApplet](http://Processing.GitHub.io/processing-javadocs/core/processing/core/PApplet.html)**

---

<div class="post-metadata">

**Author:** ![villares](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/villares/32/3166_2.png) [@villares](https://discourse.processing.org/u/villares)\
**Post date:** [December 29, 2020, 11:00pm UTC](https://discourse.processing.org/t/sizing-the-display-window-to-fit-image-dimensions/26598/6 "2020-12-29T23:00:58Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![javagar](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/javagar/32/11434_2.png) [@javagar](https://discourse.processing.org/u/javagar)\
**Post date:** [December 29, 2020, 11:02pm UTC](https://discourse.processing.org/t/sizing-the-display-window-to-fit-image-dimensions/26598/7 "2020-12-29T23:02:35Z")

</div>

> [@CodeMasterX](#):
>
> what does `this.` mean?

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.

---

<div class="post-metadata">

**Author:** ![CodeMasterX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemasterx/32/2942_2.png) [@CodeMasterX](https://discourse.processing.org/u/CodeMasterX)\
**Post date:** [December 30, 2020, 12:00am UTC](https://discourse.processing.org/t/sizing-the-display-window-to-fit-image-dimensions/26598/8 "2020-12-30T00:00:51Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![javagar](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/javagar/32/11434_2.png) [@javagar](https://discourse.processing.org/u/javagar)\
**Post date:** [December 30, 2020, 12:18am UTC](https://discourse.processing.org/t/sizing-the-display-window-to-fit-image-dimensions/26598/9 "2020-12-30T00:18:25Z")

</div>

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:

```auto
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 …

```auto
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](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/b/ba305dff0d9098c63e65ad4d765937ed83b59f95.png)
