# The get() function with width and height not working

**URL:** <https://discourse.processing.org/t/the-get-function-with-width-and-height-not-working/31613>\
**Category:** Beginners\
**Created:** [August 7, 2021, 1:25pm UTC](https://discourse.processing.org/t/the-get-function-with-width-and-height-not-working/31613 "2021-08-07T13:25:09Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![paulstgeorge](https://avatars.discourse-cdn.com/v4/letter/p/53a042/32.png) [@paulstgeorge](https://discourse.processing.org/u/paulstgeorge)\
**Post date:** [August 7, 2021, 1:25pm UTC](https://discourse.processing.org/t/the-get-function-with-width-and-height-not-working/31613/1 "2021-08-07T13:25:09Z")

</div>

The code is very simple and I am referring to \<[https://processing.org/reference/get\_.html](https://processing.org/reference/get_.html)\>

But when I try to use `get(100, 100, 5, 5)` I get the error “cannot convert from PImage to int”. Is it me, or the code?

```auto
PImage img;

void setup() {
  size(336, 336); 
  img = loadImage("flowers.jpg");
}

void draw() {
  image(img, 0, 0);

  //color c = get(100, 100, 5, 5); // this does not work
  color c = get(100, 100); // this works
  
  println(blue(c));
}

```

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [August 7, 2021, 2:11pm UTC](https://discourse.processing.org/t/the-get-function-with-width-and-height-not-working/31613/2 "2021-08-07T14:11:39Z")

</div>

Hello,

> [@paulstgeorge](#):
>
> Is it me, or the code?

A little of both?  
Sometimes we just need some insight from others.  
I usually walk away from code and come back to it later with a fresh outlook.

See the reference for details:  
_[get() / Reference / Processing.org](https://processing.org/reference/get_.html)_

It states:

> If a width and a height are specified, **get(x, y, w, h)** returns a PImage corresponding to the part of the original PImage where the top left pixel is at the **(x, y)** position with a width of **w** a height of **h**.

> **Example**
>
> ```auto
> PImage img, img2;
> 
> void setup() {
> size(400, 400); 
> img = loadImage("frog.png");
> img2 = createImage(50, 50, RGB);
>   
> }
> 
> void draw() {
> background(0);
> image(img, 0, 0);
>   
> img2 = get(mouseX-25, mouseY-25, 50, 50); // this works
> color c = get(100, 100); // this works
>   
> image(img2, 0, 300);
>   
> println(blue(c));
> }
> 
> ```

![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/6/66569b8c068c7b2cdb4b9267773c32e35707e22a.jpeg)

`:)`

---

<div class="post-metadata">

**Author:** ![monkstone](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/monkstone/32/64_2.png) [@monkstone](https://discourse.processing.org/u/monkstone)\
**Post date:** [August 7, 2021, 2:25pm UTC](https://discourse.processing.org/t/the-get-function-with-width-and-height-not-working/31613/3 "2021-08-07T14:25:18Z")

</div>

This previous discussion might be of interest to you [https://forum.processing.org/two/discussion/15573/get-the-average-rgb-from-pixels](https://forum.processing.org/two/discussion/15573/get-the-average-rgb-from-pixels). Personally I have never liked the fake type of `color` used by processing which masks the fact that it is really a `signed int`, because that also causes unnecessary confusion to beginners.

---

<div class="post-metadata">

**Author:** ![paulstgeorge](https://avatars.discourse-cdn.com/v4/letter/p/53a042/32.png) [@paulstgeorge](https://discourse.processing.org/u/paulstgeorge)\
**Post date:** [August 7, 2021, 5:13pm UTC](https://discourse.processing.org/t/the-get-function-with-width-and-height-not-working/31613/4 "2021-08-07T17:13:18Z")

</div>

> [@glv](#):
>
> `img2 = createImage(100, 100, RGB);`// or `img2 = createImage(50, 50, RGB);

Ah ha, I think I get it. Thanks. I had assumed (incorrectly) that the results were in an array.

---

<div class="post-metadata">

**Author:** ![paulstgeorge](https://avatars.discourse-cdn.com/v4/letter/p/53a042/32.png) [@paulstgeorge](https://discourse.processing.org/u/paulstgeorge)\
**Post date:** [August 7, 2021, 5:22pm UTC](https://discourse.processing.org/t/the-get-function-with-width-and-height-not-working/31613/5 "2021-08-07T17:22:35Z")

</div>

Yes, that is exactly what I am wanting to do! And pixels[] is far better than get(), but I was curious about the get() with width and height.

The post you refer to is very interesting but I will average luminosity, chroma, and hue instead of R, G, and B.

---

<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 7, 2021, 5:51pm UTC](https://discourse.processing.org/t/the-get-function-with-width-and-height-not-working/31613/6 "2021-08-07T17:51:14Z")

</div>

Yeah, get() with 2 parameters returns a color and get with 4 parameters (area) returns an image

please note that the get() on its own is referring to the screen; img.get() to the image itself which can also be invisible (not on the screen)

---

<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:** [August 7, 2021, 6:16pm UTC](https://discourse.processing.org/t/the-get-function-with-width-and-height-not-working/31613/7 "2021-08-07T18:16:27Z")

</div>

There actually seem to be at least two sources of confusion concerning the `get` function or method.

> [@monkstone](#):
>
> Personally I have never liked the fake type of `color` used by processing which masks the fact that it is really a `signed int` …

Yeah, that is notorious for fostering trouble.

> [@Chrisir](#):
>
> get() with 2 parameters returns a color and get with 4 parameters (area) returns an image

Yes, most functions or methods return a uniform type, even when they allow varying numbers of arguments. However, the `get` function, or method, is different.

To summarize:

- `get(x, y)` returns an `int` that represents a `color`.
- `get(x, y, w, h)` returns a `PImage`.
- `get()` returns a `PImage`.

---

<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:** [August 7, 2021, 6:59pm UTC](https://discourse.processing.org/t/the-get-function-with-width-and-height-not-working/31613/8 "2021-08-07T18:59:09Z")

</div>

> [@javagar](#):
>
> Yes, most functions or methods return a uniform type …

Well, the opposing view does have some merit, as in the following, from [Oracle: java.lang: Class Math](https://docs.oracle.com/javase/8/docs/api/java/lang/Math.html):

`static double` `max(double a, double b)` Returns the greater of two `double` values.  
`static float` `max(float a, float b)` Returns the greater of two `float` values.  
`static int` `max(int a, int b)` Returns the greater of two `int` values.  
`static long` `max(long a, long b)` Returns the greater of two `long` values.

Four different types are accommodated. However, in my opinion, that is intuitive. Pass in two numbers, and get back a number of the same type. The `get()` function seems less intuitive, regarding return types, which is also an opinion, of course.

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [August 7, 2021, 9:06pm UTC](https://discourse.processing.org/t/the-get-function-with-width-and-height-not-working/31613/9 "2021-08-07T21:06:03Z")

</div>

> [@Chrisir](#):
>
> please note that the get() on its own is referring to the screen; img.get() to the image itself which can also be invisible (not on the screen)

Reference is here:  
[https://processing.org/reference/PImage\_get\_.html](https://processing.org/reference/PImage_get_.html)

I like references.

`:)`

---

<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 7, 2021, 9:08pm UTC](https://discourse.processing.org/t/the-get-function-with-width-and-height-not-working/31613/10 "2021-08-07T21:08:42Z")

</div>

Yeah, and the other one: [get() \ Language (API) \ Processing 3+](https://processing.org/reference/get_.html)

---

<div class="post-metadata">

**Author:** ![paulstgeorge](https://avatars.discourse-cdn.com/v4/letter/p/53a042/32.png) [@paulstgeorge](https://discourse.processing.org/u/paulstgeorge)\
**Post date:** [August 8, 2021, 9:28am UTC](https://discourse.processing.org/t/the-get-function-with-width-and-height-not-working/31613/11 "2021-08-08T09:28:50Z")

</div>

> [@javagar](#):
>
> returns a `PImage` .

Thanks everyone. I looked at the reference but had overlooked the “returns a PImage”. It was worth it though because now I have a working sketch and have picked up /= (divide assign) and += (add assign) on the way.

I want to pick the colour from an area rather than a pixel because I am using the data for colorimetry and information from one pixel can be misleading.
