Skip to content
Related Articles

Related Articles

Image Processing in Java | Set 8 (Creating mirror image)
  • Difficulty Level : Expert
  • Last Updated : 08 Dec, 2016

We strongly recommend to refer below post as a prerequisite of this.

In this set we will be creating mirror image.
The main trick is to just get the source pixel values from left to right and set the same in the result image from right to left.


  1. Read the source image in a BufferedImage to read the given image.
  2. Get the dimensions of the given image.
  3. Create another BufferedImage object of same dimension to hold the mirror image.
  4. Get ARGB (Alpha, Red, Green and Blue) values from source image [in left to right fashion].
  5. Set ARGB (Alpha, Red, Green and Blue) to newly created image [in right to left fashion].
  6. Repeat the steps 4 and 5 for each pixels of the image.

Below is Java implementation of above steps.





// Java program to demonstrate creation of mirror image
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
public class MirrorImage
    public static void main(String args[])throws IOException
        // BufferedImage for source image
        BufferedImage simg = null;
        // File object
        File f = null;
        // Read source image file
            f = new File("G:\\Inp.jpg");
            simg =;
        catch(IOException e)
            System.out.println("Error: " + e);
        // Get source image dimension
        int width = simg.getWidth();
        int height = simg.getHeight();
        // BufferedImage for mirror image
        BufferedImage mimg = new BufferedImage(width, height,
        // Create mirror image pixel by pixel
        for (int y = 0; y < height; y++)
            for (int lx = 0, rx = width - 1; lx < width; lx++, rx--)
                // lx starts from the left side of the image
                // rx starts from the right side of the image
                // lx is used since we are getting pixel from left side
                // rx is used to set from right side
                // get source pixel value
                int p = simg.getRGB(lx, y);
                // set mirror image pixel value
                mimg.setRGB(rx, y, p);
        // save mirror image
            f = new File("G:\\Out.jpg");
            ImageIO.write(mimg, "jpg", f);
        catch(IOException e)
            System.out.println("Error: " + e);


NOTE:Code will not run on online ide since it requires image in drive.




This article is contributed by Pratik Agarwal. If you like GeeksforGeeks and would like to contribute, you can also write an article using or mail your article to See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :