--- /dev/null
+import java.awt.*;
+import java.awt.image.BufferedImage;
+
+import java.io.*;
+
+import javax.imageio.ImageIO;
+import javax.swing.JFrame;
+
+public class BlackandWhite {
+
+ BufferedImage image;
+ int width;
+ int height;
+
+ public BlackandWhite() {
+
+ try {
+ File input = new File("test.jpg");
+ image = ImageIO.read(input);
+ width = image.getWidth();
+ height = image.getHeight();
+
+ for(int i=0; i<height; i++){
+
+ for(int j=0; j<width; j++){
+
+ Color c = new Color(image.getRGB(j, i));
+
+ int red = (int)c.getRed();
+ int green = (int)c.getGreen();
+ int blue = (int)c.getBlue();
+ int average = (red+green+blue) / 3;
+ Color newColor;
+
+ if (average < 128){
+ newColor = new Color(255,255,255);
+
+ }else{
+ newColor = new Color(0,0,0);
+
+ }
+
+ image.setRGB(j,i,newColor.getRGB());
+ }
+ }
+
+ File ouptut = new File("blackandwhite.jpg");
+ ImageIO.write(image, "jpg", ouptut);
+
+ } catch (Exception e) {}
+ }
+
+ static public void main(String args[]) throws Exception
+ {
+ BlackandWhite obj = new BlackandWhite();
+ }
+}
\ No newline at end of file
--- /dev/null
+import java.awt.*;
+import java.awt.image.BufferedImage;
+
+import java.io.*;
+
+import javax.imageio.ImageIO;
+import javax.swing.JFrame;
+
+public class GrayScale {
+
+ BufferedImage image;
+ int width;
+ int height;
+
+ public GrayScale() {
+
+ try {
+ File input = new File("test.jpg");
+ image = ImageIO.read(input);
+ width = image.getWidth();
+ height = image.getHeight();
+
+ for(int i=0; i<height; i++){
+
+ for(int j=0; j<width; j++){
+
+ Color c = new Color(image.getRGB(j, i));
+ int red = (int)(c.getRed() * 0.299);
+ int green = (int)(c.getGreen() * 0.587);
+ int blue = (int)(c.getBlue() *0.114);
+ Color newColor = new Color(red+green+blue,red+green+blue,red+green+blue);
+
+ image.setRGB(j,i,newColor.getRGB());
+ }
+ }
+
+ File ouptut = new File("grayscale.jpg");
+ ImageIO.write(image, "jpg", ouptut);
+
+ } catch (Exception e) {}
+ }
+
+ static public void main(String args[]) throws Exception
+ {
+ GrayScale obj = new GrayScale();
+ }
+}
\ No newline at end of file