Saturday, 24 October 2020

Digital Image Processor in Java

  • Start your IDE and type the following code in a class named Digital_Image_Processing

import java.awt.Color;

import java.awt.Image;

import java.awt.Toolkit;

import java.awt.image.BufferedImage;

import java.io.File;

import java.io.IOException;

import java.util.Scanner;

 

import javax.imageio.ImageIO;

import javax.swing.JFrame;

import javax.swing.JTextArea;

 

public class Picasso {

 

       @SuppressWarnings("resource")

 

       public static void main(String[] args) throws IOException {

              for (int time = 0;; time++) {

                     System.out.println("Enter image source");

                     File file;

                     Image xyz;

                     for (;;) {

                           for (;;) {

                                  file = new File((new Scanner(System.in)).nextLine());

                                  if (file.exists() == true) {

                                         break;

                                  }

                                  System.err.println("Your file doesn't exist. Please check your file address and try again.");

                           }

                           xyz = ImageIO.read(file);

                           if (xyz != null) {

                                  break;

                           }

                           System.err.println("Your file is not compatible with system. Please try again.");

                     }

                     JFrame frm = new JFrame();

                     frm.setSize(xyz.getWidth(null) + 25, xyz.getHeight(null) + 50);

                     frm.setAlwaysOnTop(true);

                     frm.setLayout(null);

                     frm.setResizable(false);

                     int choice = 0;

                     if (time == 0) {

                           System.out.println("Welcome to Picasso");

                     }

                     System.out.println("Enter:");

                     System.out.println("0 for original image");

                     System.out.println("1 for dot-pattern");

                     System.out.println("2 for negatification");

                     System.out.println("3 for pixelate");

                     System.out.println("4 for high-contrast");

                     System.out.println("5 for semi-contrast");

                     System.out.println("6 for black-&-white");

                     System.out.println("7 for vertical mode");

                     System.out.println("8 for horizontal mode");

                     System.out.println("9 for resize");

                     System.out.println("10 for flip");

                     System.out.println("11 for skew");

                     System.out.println("12 for erratic mode");

                     System.out.println("Modes are only for images small in size and ");

                     System.out.println("large images may take longer to process.");

                     for (;;) {

                           choice = (new Scanner(System.in)).nextInt();

                           if (choice >= 0 && choice <= 12) {

                                  break;

                           }

                           System.err.println("Invalid choice. Please try again.");

                     }

                     //Original

                     if (choice == 0) {

                           frm.setTitle("Original");

                           int z = 0;

                           for (int i = 0; i < xyz.getHeight(null); i++) {

                                  for (int j = 0; j < xyz.getWidth(null); j++) {

                                         JTextArea x = new JTextArea();

                                         x.setEditable(false);

                                         x.setVisible(true);

                                         x.setLocation(j+5, i+5);

                                         x.setSize(1, 1);

                                         int abc = ((BufferedImage) xyz).getRGB(j, i);

                                         x.setBackground(new Color(abc));

                                         frm.add(x);

                                         z++;

                                         System.out.println(z * 100 / ((xyz.getHeight(null)) * (xyz.getWidth(null))) + "% completed.");

                                  }

                           }

                     }

                     //Dot

                     if (choice == 1) {

                           System.out.println("Enter dot value");

                           int pix = (new Scanner(System.in)).nextInt();

                           frm.setTitle("Dot");

                           int z = 0;

                           for (int i = 0; i < xyz.getHeight(null) / pix; i++) {

                                  for (int j = 0; j < xyz.getWidth(null) / pix; j++) {

                                         JTextArea x = new JTextArea();

                                         x.setEditable(false);

                                         x.setVisible(true);

                                         x.setLocation(j * pix+5, i * pix+5);

                                         x.setSize(1, 1);

                                         int abc = ((BufferedImage) xyz).getRGB(j * pix, i * pix);

                                         x.setBackground(new Color(abc));

                                         frm.add(x);

                                         z++;

                                         System.out.println(z * 100 / (xyz.getHeight(null) / pix * (xyz.getWidth(null) / pix)) + "% completed.");

                                  }

                           }

 

                     }

                     //Negatify

                     if (choice == 2) {

                           frm.setTitle("Negatify");

                           int z = 0;

                           for (int i = 0; i < xyz.getHeight(null); i++) {

                                  for (int j = 0; j < xyz.getWidth(null); j++) {

                                         JTextArea x = new JTextArea();

                                         x.setEditable(false);

                                         x.setVisible(true);

                                         x.setLocation(j+5, i+5);

                                         x.setSize(1, 1);

                                         int abc = -((BufferedImage) xyz).getRGB(j, i);

                                         x.setBackground(new Color(abc));

                                         frm.add(x);

                                         z++;

                                         System.out.println(z * 100 / ((xyz.getHeight(null)) * (xyz.getWidth(null))) + "% completed.");

                                  }

                           }

                     }

                     //Pixelate

                     if (choice == 3) {

                           System.out.println("Enter pixel value");

                           int pix = (new Scanner(System.in)).nextInt();

                           frm.setTitle("Pixelate");

                           int z = 0;

                           for (int i = 0; i < xyz.getHeight(null) / pix; i++) {

                                  for (int j = 0; j < xyz.getWidth(null) / pix; j++) {

                                         JTextArea x = new JTextArea();

                                         x.setEditable(false);

                                         x.setVisible(true);

                                         x.setLocation(j * pix+5, i * pix+5);

                                         x.setSize(pix, pix);

                                         int abc = ((BufferedImage) xyz).getRGB(j * pix, i * pix);

                                         x.setBackground(new Color(abc));

                                         frm.add(x);

                                         z++;

                                         System.out.println(z * 100 / (xyz.getHeight(null) / pix * (xyz.getWidth(null) / pix)) + "% completed.");

                                  }

                           }

                     }

                     //High-contrast

                     if (choice == 4) {

                           System.out.println("Enter contrast mode (1-3)");

                           int cont = (new Scanner(System.in)).nextInt();

                           int z = 0;

                           frm.setTitle("High-contrast: Mode " + cont);

                           for (int i = 0; i < xyz.getHeight(null); i++) {

                                  for (int j = 0; j < xyz.getWidth(null); j++) {

                                         int abc = ((BufferedImage) xyz).getRGB(j, i);

                                         if (cont == 1) {

                                                abc = (new Color(abc)).getBlue();

                                         }

                                         if (cont == 2) {

                                                abc = (new Color(abc)).getGreen();

                                         }

                                         if (cont == 3) {

                                                abc = (new Color(abc)).getRed();

                                         }

                                         z++;

                                         System.out.println(z * 100 / ((xyz.getHeight(null)) * (xyz.getWidth(null))) + "% completed.");

                                         if (abc > 127.5) {

                                                continue;

                                         }

                                         JTextArea x = new JTextArea();

                                         x.setEditable(false);

                                         x.setVisible(true);

                                         x.setLocation(j+5, i+5);

                                         x.setSize(1, 1);

                                         if (abc < 127.5) {

                                                x.setBackground(Color.black);

                                         }

                                         frm.add(x);

                                  }

                           }

 

                     }

                     //Semi-contrast

                     if (choice == 5) {

                           System.out.println("Enter contrast mode (1-3)");

                           int cont = (new Scanner(System.in)).nextInt();

                           int z = 0;

                           frm.setTitle("Semi-contrast: Mode " + cont);

                           for (int i = 0; i < xyz.getHeight(null); i++) {

                                  for (int j = 0; j < xyz.getWidth(null); j++) {

                                         int abc = ((BufferedImage) xyz).getRGB(j, i);

                                         if (cont == 1) {

                                                abc = (new Color(abc)).getBlue();

                                         }

                                         if (cont == 2) {

                                                abc = (new Color(abc)).getGreen();

                                         }

                                         if (cont == 3) {

                                                abc = (new Color(abc)).getRed();

                                         }

                                         z++;

                                         System.out.println(z * 100 / ((xyz.getHeight(null)) * (xyz.getWidth(null))) + "% completed.");

                                         if (abc >= 150) {

                                                continue;

                                         }

                                         JTextArea x = new JTextArea();

                                         x.setEditable(false);

                                         x.setVisible(true);

                                         x.setLocation(j+5, i+5);

                                         x.setSize(1, 1);

                                         if (abc < 75) {

                                                x.setBackground(Color.black);

                                         }

                                         if (abc >= 75 & abc < 150) {

                                                x.setBackground(Color.gray);

                                         }

                                         frm.add(x);

                                  }

                           }

                     }

                     //Black-&-White

                     if (choice == 6) {

                           System.out.println("Enter B/W mode (1-3)");

                           int cont = (new Scanner(System.in)).nextInt();

                           int z = 0;

                           frm.setTitle("Black-&-White: Mode " + cont);

                           for (int i = 0; i < xyz.getHeight(null); i++) {

                                  for (int j = 0; j < xyz.getWidth(null); j++) {

                                         int abc = ((BufferedImage) xyz).getRGB(j, i);

                                         if (cont == 1) {

                                                abc = (new Color(abc)).getBlue();

                                         }

                                         if (cont == 2) {

                                                abc = (new Color(abc)).getGreen();

                                         }

                                         if (cont == 3) {

                                                abc = (new Color(abc)).getRed();

                                         }

                                         z++;

                                         System.out.println(z * 100 / ((xyz.getHeight(null)) * (xyz.getWidth(null))) + "% completed.");

                                         if (abc >= 180) {

                                                continue;

                                         }

                                         JTextArea x = new JTextArea();

                                         x.setEditable(false);

                                         x.setVisible(true);

                                         x.setLocation(j+5, i+5);

                                         x.setSize(1, 1);

                                         if (abc < 45) {

                                                x.setBackground(Color.black);

                                         }

                                         if (abc >= 45 & abc < 90) {

                                                x.setBackground(new Color(64, 64, 64));

                                         }

                                         if (abc >= 90 & abc < 135) {

                                                x.setBackground(Color.gray);

                                         }

                                         if (abc >= 135 & abc < 180) {

                                                x.setBackground(new Color(184, 184, 184));

                                         }

                                         frm.add(x);

                                  }

                           }

                     }

                     //Vertical

                     if (choice == 7) {

                           System.out.println("Enter vertical value");

                           int pix = (new Scanner(System.in)).nextInt();

                           frm.setTitle("Vertical");

                           int z = 0;

                           for (int i = 0; i < xyz.getHeight(null) / pix; i++) {

                                  for (int j = 0; j < xyz.getWidth(null) / pix; j++) {

                                         JTextArea x = new JTextArea();

                                         x.setEditable(false);

                                         x.setVisible(true);

                                         x.setLocation(j * pix+5, i * pix+5);

                                         x.setSize(1, pix);

                                         int abc = ((BufferedImage) xyz).getRGB(j * pix, i * pix);

                                         x.setBackground(new Color(abc));

                                         frm.add(x);

                                         z++;

                                         System.out.println(z * 100 / (xyz.getHeight(null) / pix * (xyz.getWidth(null) / pix)) + "% completed.");

                                  }

                           }

 

                     }

                     //Horizontal

                     if (choice == 8) {

                           System.out.println("Enter horizontal value");

                           int pix = (new Scanner(System.in)).nextInt();

                           frm.setTitle("Horizontal");

                           int z = 0;

                           for (int i = 0; i < xyz.getHeight(null) / pix; i++) {

                                  for (int j = 0; j < xyz.getWidth(null) / pix; j++) {

                                         JTextArea x = new JTextArea();

                                         x.setEditable(false);

                                         x.setVisible(true);

                                         x.setLocation(j * pix+5, i * pix+5);

                                         x.setSize(pix, 1);

                                         int abc = ((BufferedImage) xyz).getRGB(j * pix, i * pix);

                                         x.setBackground(new Color(abc));

                                         frm.add(x);

                                         z++;

                                         System.out.println(z * 100 / (xyz.getHeight(null) / pix * (xyz.getWidth(null) / pix)) + "% completed.");

                                  }

                           }

                     }

                     //Resize

                     if (choice == 9) {

                           System.out.println("Enter height percentage");

                           double ht = (new Scanner(System.in)).nextDouble() / 100;

                           System.out.println("Enter width percentage");

                           double wid = (new Scanner(System.in)).nextDouble() / 100;

                           frm.setSize((int) Math.ceil(xyz.getWidth(null) * wid) + 25, (int) Math.ceil(xyz.getHeight(null) * ht) + 50);

                           frm.setTitle("H: "+ht*100+"% W: "+wid*100+"%");

                           for (int i = 0; i < (xyz.getHeight(null)); i++) {

                                  for (int j = 0; j < (xyz.getWidth(null)); j++) {

                                         JTextArea x = new JTextArea();

                                         x.setEditable(false);

                                         x.setVisible(true);

                                         x.setLocation((int) Math.ceil(j * wid)+5, (int) Math.ceil(i * ht));

                                         x.setSize((int) Math.ceil(wid), (int) Math.ceil(ht));

                                         int abc = ((BufferedImage) xyz).getRGB(j, i);

                                         x.setBackground(new Color(abc));

                                         frm.add(x);

                                  }

                           }

                     }

                     //Flip

                     if (choice == 10) {

                           System.out.println("Enter:");

                           System.out.println("0 for horizontal flip");

                           System.out.println("1 for vertical flip");

                           System.out.println("2 for both");

                           System.out.println();

                           int num=(new Scanner(System.in)).nextInt();

                           frm.setTitle("Horizontal Flip");

                           for (int i = 0; i < xyz.getHeight(null); i++) {

                                  for (int j = 0; j < xyz.getWidth(null); j++) {

                                         JTextArea x = new JTextArea();

                                         x.setEditable(false);

                                         x.setVisible(true);

                                         if (num==0) {

                                                x.setLocation(xyz.getWidth(null)+4-j, i+5);

                                         }

                                         if (num==1) {

                                                x.setLocation(j+5, xyz.getHeight(null)+4-i);

                                         }

                                         if (num==2) {

                                                x.setLocation(xyz.getWidth(null)+4-j, xyz.getHeight(null)+4-i);

                                         }

                                         x.setSize(1, 1);

                                         int abc = ((BufferedImage) xyz).getRGB(j, i);

                                         x.setBackground(new Color(abc));

                                         frm.add(x);

                                  }

                           }

                     }

                     //Skew

                     if (choice==11) {

                           frm.setResizable(true);

                           System.out.println("Enter integer value of image skew");

                           int val=(new Scanner(System.in)).nextInt();

                           frm.setTitle("Skew: "+val);

                           System.out.println("Enter:");

                           System.out.println("0 for horizontal skew");

                           System.out.println("1 for vertical skew");

                           int hv=(new Scanner(System.in)).nextInt();

                           int sk=0;

                           frm.setSize(Toolkit.getDefaultToolkit().getScreenSize());

                           if (hv==0) {

                                  System.out.println("Enter:");

                                  System.out.println("0 for Top-Right skew");

                                  System.out.println("1 for Bottom-Right skew");

                                  sk=(new Scanner(System.in)).nextInt();

                                  if (sk==0) {

                                         frm.setTitle("H: T-R");

                                  }

                                  if (sk==1) {

                                         frm.setTitle("H: B-R");

                                  }

                           }

                           if (hv==1) {

                                  System.out.println("Enter:");

                                  System.out.println("0 for Right-Bottom skew");

                                   System.out.println("1 for Right-Top skew");

                                  sk=(new Scanner(System.in)).nextInt();

                                  if (sk==0) {

                                         frm.setTitle("V: R-B");

                                  }

                                  if (sk==1) {

                                         frm.setTitle("V: R-T");

                                  }

                           }

                           for (int i = 0; i < xyz.getHeight(null); i++) {

                                  for (int j = 0; j < xyz.getWidth(null); j++) {

                                         JTextArea x = new JTextArea();

                                         x.setEditable(false);

                                         x.setVisible(true);

                                         if (hv==0 && sk==0) {

                                                x.setLocation(5+j+(xyz.getHeight(null)-1-i)*val/45, 5+i);// H T-R

                                         }

                                         if (hv==0 && sk==1) {

                                                x.setLocation(5+j+i*val/45, 5+i);// H B-R

                                         }

                                         if (hv==1 && sk==0) {

                                                x.setLocation(5+j, 5+i+j*val/45);// V R-B

                                         }

                                         if (hv==1 && sk==1) {

                                                x.setLocation(5+j, 5+i+(xyz.getWidth(null)-1-j)*val/45);// V R-T

                                         }

                                         x.setSize(1, 1);

                                         int abc = ((BufferedImage) xyz).getRGB(j, i);

                                         x.setBackground(new Color(abc));

                                         frm.add(x);

                                  }

                           }

                     }

                     //Erratic

                     if (choice == 12) {

                           frm.setTitle("Erratic");

                           System.out.println("Enter erraticity");

                           double e=(new Scanner(System.in)).nextDouble();

                           int z = 0;

                           for (int i = 0; i < xyz.getHeight(null); i++) {

                                  for (int j = 0; j < xyz.getWidth(null); j++) {

                                         JTextArea x = new JTextArea();

                                         x.setEditable(false);

                                         x.setVisible(true);

                                         x.setLocation(j+5, i+5);

                                         x.setSize((int)Math.ceil(Math.random()*e),(int)Math.ceil(Math.random()*e));

                                         int abc = ((BufferedImage) xyz).getRGB(j, i);

                                         x.setBackground(new Color(abc));

                                         frm.add(x);

                                         z++;

                                         System.out.println(z * 100 / ((xyz.getHeight(null)) * (xyz.getWidth(null))) + "% completed.");

                                  }

                           }

                     }

                     frm.setVisible(true);

                     System.out.println("------------------------");

                     System.out.println("Do you want to continue?(Y/N)");

                     if ((new Scanner(System.in)).nextLine().equalsIgnoreCase("N")) {

                           System.exit(0);

                     }

                     System.out.println("------------------------");

              }

 

       }

 

 

}

 

 

  •  Execute the code.

Algorithmic Pixel Art: The Beauty of Aliasing

Discussed  here More images in  Google drive Some outputs: Forward to anyone who says aliasing is ugly.