How to resize my loadImage?

I originally got a program that reads the pixels in a picture, In order for it to work the picture has to be the same size as the canvas or for you to see it. Well I loaded in a 1920 x 1080 picture and loaded it into a 1000 x 500 canvas, I set the images (image(img, 0, 0, width, height);) size to the height and the width of the canvas, but when I load the picture using the loadImage method it loads the image in its original resolution in the background where I cant see it, I want it to load the image into the size of the canvas, how can I accomplish this? Code:

PImage img;
int direction = 1;
float signal;
int imgX = 0;
int imgY = 0;
int w = width;
int h = height;

void setup() {
  size(1000, 500);

void draw() {
  // int boxX = img.width/2;
  //int boxY = img.height/2;
  img = loadImage("203.jpg");


  if (mousePressed) {
    int mx = constrain(mouseX, 0, width-1);
    int my = constrain(mouseY, 0, height-1);
    signal = my*width + mx;
  } else {
    //signal += 0.33*direction;

 // set(imgX, imgY, img);  // fast way to draw an image

  int sx = int(signal) % width;
  int sy = int(signal) / width;
  color c = img.get(sx, sy);
  image(img, 0, 0, width, height);

  //rect(sx, sy, 10, 10);
  rect(50, 50, 50, 50);

  print("Color " + c);
  println(" X " + sx + " Y " + sy );

In order for this code to work you have to set the canvas size to the resolution of the image, and I do not want that. Plus the canvas size cannot be re-sized.

If you run this program at the top left corner you will see a box that will copy the color of the pixel where your mouse is at, hover over any pixel to see what my problem is.


  • Your image will always be loaded at whatever size it is. If you want to load your image so it is a different size, you will need to change the size of the image file itself!

    Of course, you can always specify the size that you want to draw it at when you call image(). So there's really no need to resize it beforehand unless you're concerned that it is using up a lot of memory.

  • Tried resizing and it didn't work

  • edited January 2016

    Oh, and



  • Tried resizing and it didn't work

    i tried it and it did work

    void setup() {
      size(1000, 500);
      img = loadImage("test.jpg");
      img.resize(width, height);

    and remove the loadImage() from draw()

Sign In or Register to comment.