Image Processing in Java | Set 7 (Creating a random pixel image)
We strongly recommend to refer below post as a prerequisite of this.
- Image Processing in Java | Set 1 (Read and Write)
- Image Processing In Java | Set 2 (Get and set Pixels)
In this set we will be creating a random pixel image. For creating a random pixel image, we don’t need any input image We can create an image file and set its pixel values generated randomly.
Algorithm:
- Set the dimension of new image file.
- Create a BufferedImage object to hold the image [ import java.awt.image.BufferedImage; ]. This object is used to store an image in RAM.
- Generate random number values for alpha, red, green and blue components.
- Set the randomly generated ARGB (Alpha, Red, Green and Blue) values.
- Repeat the steps 3 and 4 for each pixels of the image.
Implementation of the above algorithm:
// Java program to demonstrate creation of random pixel image import java.io.File; import java.io.IOException; import java.awt.image.BufferedImage; import javax.imageio.ImageIO; public class RandomImage { public static void main(String args[])throws IOException { // Image file dimensions int width = 640, height = 320; // Create buffered image object BufferedImage img = null; img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); // file object File f = null; // create random values pixel by pixel for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { int a = (int)(Math.random()*256); //generating int r = (int)(Math.random()*256); //values int g = (int)(Math.random()*256); //less than int b = (int)(Math.random()*256); //256 int p = (a<<24) | (r<<16) | (g<<8) | b; //pixel img.setRGB(x, y, p); } } // write image try { f = new File("G:\\Out.jpg"); ImageIO.write(img, "jpg", f); } catch(IOException e) { System.out.println("Error: " + e); } } } |
Note : Code will not run on online ide since it writes image in drive.
Output:
Out.jpg![]()
This article is contributed by Pratik Agarwal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. 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.
Recommended Posts:
- Image Processing in Java | Set 8 (Creating mirror image)
- Image Processing in Java | Set 3 (Colored image to greyscale image conversion)
- Image Processing in Java | Set 4 (Colored image to Negative image conversion)
- Image Processing in Java | Set 6 (Colored image to Sepia image conversion)
- Image Processing in Java | Set 5 (Colored to Red Green Blue Image Conversion)
- Image Processing in Java | Set 11 (Changing orientation of image)
- Image Processing in Java | Set 10 ( Watermarking an image )
- Getting started with Scikit-image: image processing in Python
- Image Processing In Java | Set 2 (Get and set Pixels)
- Image Processing in Java | Set 14 ( Comparison of two images )
- Image Processing in Java | Set 1 (Read and Write)
- Image Processing in Java | Set 9 ( Face Detection )
- Image Processing in Java | Set 12 ( Contrast Enhancement )
- Image Processing using OpenCV in Java | Set 14 ( Sharpness Enhancement )
- Image Processing using OpenCV in Java | Set 13 (Brightness Enhancement)


