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));
}
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));
}
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.
static doublemax(double a, double b) Returns the greater of two double values. static floatmax(float a, float b) Returns the greater of two float values. static intmax(int a, int b) Returns the greater of two int values. static longmax(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.
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.