This program converts an image from text format to .png image format.
Usage: java TextImage input_file output_file.
The defaults are input_file=text1.txt and output_file=text.png
These small images are typically used to test image processing routines because they are small enough to
print as a matrix. See listImage.java
You can enlarge the output by replicating, as shown by showSmImage.java, which was used to
generate the following image of text.png
TextImage.javaimport java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.File;
import java.awt.image.*;
import javax.imageio.*;
public class TextImage {
public static void main(String[] args) {
String inf, outf;
if (args.length<1) inf = "text1.txt";
else inf = args[0];
if (args.length<2) outf = "text.png";
else outf = args[1];
int type = BufferedImage.TYPE_INT_RGB;
BufferedImage out = null;
int value[] = null;
int v,r,g,b;
int npix=0;
int j=0;
try (
BufferedReader br = new BufferedReader(new FileReader(inf));
) {
String s;
while ((s = br.readLine()) != null) {
String [] tokens = s.trim().split("[\\s]+");
if (out==null) {
npix = tokens.length;
System.out.format("image size %d x %d\n",npix,npix);
out = new BufferedImage(npix,npix,type);
value = new int[npix];
}
for (int i=0; i<npix; i++) {
v = Integer.parseInt(tokens[i])&0xFF;
value[i] = 0xFF000000 | (v << 16) | (v << 8) | v;
}
out.setRGB(0, j++, npix, 1, value, 0, npix);
}
br.close();
} catch (IOException e) {
System.out.println("file not found: " + inf);
//e.printStackTrace();
}
try {
ImageIO.write(out, "png", new File(outf));
} catch (IOException e) {
System.out.println("image write failed");
System.out.println(e);
System.exit(-1);
}
}
}
Maintained by John Loomis, updated Sat Mar 10 14:53:11 2018