-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImg_to_Ascii_art.java
62 lines (57 loc) · 1.89 KB
/
Img_to_Ascii_art.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
52
53
54
55
56
57
58
59
60
61
62
package com.codex.fxproject;
import javax.imageio.ImageIO;
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
public class Img_to_Ascii_art {
public static String ascii(double pix){
String str = " ";
if (pix >= 240) {
str = " ";
} else if (pix >= 210) {
str = ".";
} else if (pix >= 190) {
str = "*";
} else if (pix >= 170) {
str = "+";
} else if (pix >= 120) {
str = "^";
} else if (pix >= 110) {
str = "&";
} else if (pix >= 80) {
str = "8";
} else if (pix >= 60) {
str = "#";
} else {
str = "@";
}
return str;
}
public static void main(String[] args) throws IOException {
//inserting image file
int width = 40;
int height = 60;
BufferedImage image =null;
try {
File fin = new File("D:/IO/penguin1.png");
image = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_RGB);
image = ImageIO.read(fin);
System.out.println("Reading complete");
}catch(IOException e){
System.out.println("Error : " + e);
}
//file creating to store ascii art
PrintWriter printWriter = new PrintWriter("D:/IO/ascii_art_penguin1.txt");
double pixval = 0;
for (int i = 0; i < image.getHeight(); i++) {
for (int j = 0; j < image.getWidth(); j++) {
Color pixcol = new Color(image.getRGB(j, i));
pixval = (((pixcol.getRed()) + (pixcol.getBlue()) + (pixcol
.getGreen()))/3.0);
printWriter.print(ascii(pixval));
}
}
}
}