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;
}
A collection of handy code snippets in the languages that I use on a daily basis.
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:
Labels:
bitmap,
graphic,
images,
java,
processing,
processing.org
Subscribe to:
Post Comments (Atom)
Hi,
ReplyDeleteYour 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;
}