Skip to content

Instantly share code, notes, and snippets.

@nektro
Last active April 13, 2016 03:18
Show Gist options
  • Save nektro/b86ad6329c1dd5d13a76680960e5c0f6 to your computer and use it in GitHub Desktop.
Save nektro/b86ad6329c1dd5d13a76680960e5c0f6 to your computer and use it in GitHub Desktop.
Turn images into life files
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import javax.imageio.ImageIO;
public class Lifeinator
{
public static void main(String[] args)
{
try {
File f = new File(args[0]);
File o = new File("output.life");
BufferedImage bi = ImageIO.read(f);
FileWriter w = new FileWriter(o);
for (int i = 0; i < bi.getHeight(); i++) {
for (int j = 0; j < bi.getWidth(); j++) {
Color c = new Color(bi.getRGB(j, i));
int ca = (c.getRed() + c.getGreen() + c.getBlue()) / 3;
if (ca < 127) {
w.append("*");
}
else {
w.append("-");
}
}
w.append("\n");
}
w.close();
System.out.println("done");
}
catch (IOException e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment