I am having trouble mirroring the left half of my picture using pixels.

edited April 2017 in Questions about Code

Here is my code. How am I able to mirror the left half of the picture using pixels?

PImage img;

void setup() {
  img = loadImage("pic.JPG");

}

void draw() {
  loadPixels(); 

  img.loadPixels(); 
  for (int y = 0; y < height; y++) {
    for (int x = 0; x < width; x++) {
      int iloc = x + y*width;
      int cloc = (width - 1 - x) + y*width;      
      pixels[cloc] =  img.pixels[iloc];        
    }
  }
  updatePixels();
}
Tagged:

Answers

  • You're close. I think the real issue is that your for loop for x goes from 0 to width, not to width/2.

    I had to change a few things to get it to work, but this runs fine for me:

    PImage img;
    
    void setup() { 
      String http = "http://";
      img = loadImage( http + "s3-us-west-1.amazonaws.com/powr/defaults/image-slider2.jpg"); 
      size(1000,500);
    }
    
    void draw() { 
      loadPixels();
    
      img.loadPixels(); 
      for (int y = 0; y < img.height; y++) { 
        for (int x = 0; x < img.width/2; x++) { 
          int iloc = x + y * img.width; 
          int cloc = (img.width - 1 - x) + y * img.width;
          pixels[iloc] = img.pixels[iloc];
          pixels[cloc] = img.pixels[iloc];
        }
      } 
      updatePixels();
    }
    
  • Answer ✓

    Also, consider this:

    PImage img;
    
    void setup() { 
      String http = "http://";
      img = loadImage( http + "s3-us-west-1.amazonaws.com/powr/defaults/image-slider2.jpg"); 
      size(1000, 500);
    }
    
    void draw() { 
      image(img.get(0, 0,img.width/2, img.height), 0, 0, width/2, height);
      translate(width, 0);
      scale(-1, 1);
      image(img.get(0, 0,img.width/2, img.height), 0, 0, width/2, height);
    }
    
Sign In or Register to comment.