Open In App
Related Articles

Image Processing in Java – Creating a Random Pixel Image

Improve Article
Improve
Save Article
Save
Like Article
Like

Prerequisites:

In this article, 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.

A random image is an image in which the pixels are chosen at random, so they can take any color from the desired palette (generally 16 million colors). The resulting images look like multi-colored noise backgrounds.

Algorithm: 

  1. Set the dimension of the new image file.
  2. Create a BufferedImage object to hold the image. This object is used to store an image in RAM.
  3. Generate random number values for alpha, red, green, and blue components.
  4. Set the randomly generated ARGB (Alpha, Red, Green, and Blue) values.
  5. Repeat steps 3 and 4 for each pixel of the image.

Implementation:

Java




// 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++)
            {
                  // generating values less than 256
                int a = (int)(Math.random()*256);
                int r = (int)(Math.random()*256);
                int g = (int)(Math.random()*256); 
                int b = (int)(Math.random()*256); 
  
                  //pixel
                int p = (a<<24) | (r<<16) | (g<<8) | b; 
  
                img.setRGB(x, y, p);
            }
        }
  
        // write image
        try
        {
            f = new File("C:/Users/hp/Desktop/Image Processing in Java/gfg-logo.png");
            ImageIO.write(img, "png", f);
        }
        catch(IOException e)
        {
            System.out.println("Error: " + e);
        }
    }
}


Output:

Note: Code will not run on online ide since it writes image in drive.

If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@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. 


Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 14 Nov, 2021
Like Article
Save Article
Previous
Next
Similar Reads
Complete Tutorials