-
Notifications
You must be signed in to change notification settings - Fork 57
/
RGB_to_Gray.java
51 lines (40 loc) · 1.32 KB
/
RGB_to_Gray.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
public class RGB_to_Gray {
public static void main(String [] args){
BufferedImage img = null;
File f = null;
try{
f = new File ("C:\\Users\\Elridge\\Desktop\\COLLAGE\\mountain.jpg");
img = ImageIO.read(f);
}catch(IOException e){
System.out.println(e);
}
int width = img.getWidth();
int height = img.getHeight();
for(int y = 0; y < height; y++){
for(int x = 0; x < width; x++){
}
}
for(int y = 0; y < height; y++){
for(int x = 0; x < width; x++){
int p = img.getRGB(x,y);
int a = (p>>24)&0xff;
int r = (p>>16)&0xff;
int g = (p>>8)&0xff;
int b = p&0xff;
int avg = (r+g+b)/3;
p = (a<<24) | (avg<<16) | (avg<<8) | avg;
img.setRGB(x, y, p);
}
}
try{
f = new File ("C:\\Users\\Elridge\\Desktop\\COLLAGE\\mountain_gray.jpg");
ImageIO.write(img, "jpg", f);
}catch(IOException e){
System.out.println(e);
}
}
}