forked from pandeyprem/DSA-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimage processing.java
More file actions
64 lines (53 loc) · 1.3 KB
/
image processing.java
File metadata and controls
64 lines (53 loc) · 1.3 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
// Java program to demonstrate
// colored to grayscale conversion
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class Grayscale {
public static void main(String args[])
throws IOException
{
BufferedImage img = null;
File f = null;
// read image
try {
f = new File(
"C:/Users/hp/Desktop/Image Processing in Java/gfg-logo.png");
img = ImageIO.read(f);
}
catch (IOException e) {
System.out.println(e);
}
// get image's width and height
int width = img.getWidth();
int height = img.getHeight();
// convert to grayscale
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
// Here (x,y)denotes the coordinate of image
// for modifying the pixel value.
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;
// calculate average
int avg = (r + g + b) / 3;
// replace RGB value with avg
p = (a << 24) | (avg << 16) | (avg << 8)
| avg;
img.setRGB(x, y, p);
}
}
// write image
try {
f = new File(
"C:/Users/hp/Desktop/Image Processing in Java/GFG.png");
ImageIO.write(img, "png", f);
}
catch (IOException e) {
System.out.println(e);
}
}
}