Get() with width and height not working

The code is very simple and I am referring to <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?

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));
}


Hello,

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

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
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

:)

1 Like

This previous discussion might be of interest to you 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.

1 Like

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

2 Likes

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.

1 Like

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)

3 Likes

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

Yeah, that is notorious for fostering trouble.

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.
1 Like

Well, the opposing view does have some merit, as in the following, from Oracle: java.lang: Class Math:

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.

1 Like

Reference is here:
https://processing.org/reference/PImage_get_.html

I like references.

:)

1 Like

Yeah, and the other one: get() \ Language (API) \ Processing 3+

1 Like

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.

2 Likes