Cannot understand PImage

//Pimage test
/* Trying to undersand Pimage
created a Pimage object named img
the idea is to acquire data from the Teensy
microcontroller and fill the img area.

line 27 fills the image with a stored jpg image
line 30 places the image in the 400x400 window at a specified location

if the location position is image(img,0,0)
the below example works fine. If you move the position
to image(img,75,75) the program does not work correctly.

Why?

*/

PImage img;
int i;
int x = 0;
int y = 0;
int loc;

void setup() {
size(400,400);
img = loadImage(“Test256.jpg”);
background(90);
//image(img,0,0);
image(img,50,50);
img.loadPixels();
textSize(24);
}

void draw() {
mousePos();
fillColor();
}

void mousePos() {
fill(50);
rect(185,360,50,25);
fill(255);
text(“mouseX”,100,380);
text(mouseX,190,380);
fill(50);
rect(340,360,50,25);
fill(255);
text(“mouseY”,250,380);
text(mouseY,340,380);
}

void fillColor() {
color c = img.get(mouseX,mouseY);
fill(c);
rect(0,350,50,50);
}

Hello @MSousa,

Please format your code as per instructions here:
https://discourse.processing.org/faq#format-your-code

Consider:

color c = img.get(mouseX, mouseY); // Gets the color or the pixel in the PImage img
//color c = get(mouseX, mouseY);   // Gets the color of pixel in the sketch window

References for get (there are two of them):

https://processing.org/reference/get_.html
https://processing.org/reference/PImage_get_.html

You could also add an offset here:

color c = img.get(mouseX - offX, mouseY - offY); // Gets the color or the pixel in the PImage img

Think about that last one and how much you want to offset.

:)

1 Like