April 23, 2012

Processing: Flip a PImage horizontally

I needed a mirror image of a PImage in my Processing project, so I came up with this little snippet:
public PImage getReversePImage( PImage image ) {
 PImage reverse = new PImage( image.width, image.height );
 for( int i=0; i < image.width; i++ ){
  for(int j=0; j < image.height; j++){
   reverse.set( image.width - 1 - i, j, image.get(i, j) );
  }
 }
 return reverse;
}

1 comment:

  1. Hi,

    Your snippet works well, thanks :)
    This a modified version which keeps alpha.

    public PImage getReversePImage( PImage image ) {
    PImage reverse;
    reverse = createImage(image.width, image.height,ARGB );

    for( int i=0; i < image.width; i++ ){
    for(int j=0; j < image.height; j++){
    int xPixel, yPixel;
    xPixel = image.width - 1 - i;
    yPixel = j;
    reverse.pixels[yPixel*image.width+xPixel]=image.pixels[j*image.width+i] ;
    }
    }
    return reverse;
    }

    ReplyDelete