Skip to content

Instantly share code, notes, and snippets.

@Geek8080
Last active June 29, 2020 11:49
Show Gist options
  • Save Geek8080/a495a68efb45d912d2ed577f6f8dd70c to your computer and use it in GitHub Desktop.
Save Geek8080/a495a68efb45d912d2ed577f6f8dd70c to your computer and use it in GitHub Desktop.

Revisions

  1. Geek8080 revised this gist Jun 29, 2020. 1 changed file with 19 additions and 11 deletions.
    30 changes: 19 additions & 11 deletions ASCIIArt.java
    Original file line number Diff line number Diff line change
    @@ -22,7 +22,11 @@ public static void main(String[] args) throws Exception {

    if (i == 1) {
    System.out.print("Enter File Name including the Extension: ");
    asciiArtifyImage(reader.readLine().trim());
    String name = reader.readLine().trim();
    System.out.print("Do you want high-accuracy output(y/n): ");
    boolean hd = false;
    hd = reader.readLine().trim().equalsIgnoreCase("y");
    asciiArtifyImage(name, hd);
    }

    if (i == 2) {
    @@ -63,9 +67,13 @@ public static void asciiArtifyText(String text) throws FileNotFoundException {
    writeBWArt(image);
    }

    public static void asciiArtifyImage(String fileName) throws Exception {
    public static void asciiArtifyImage(String fileName, boolean hd) throws Exception {
    File im = new File(fileName);
    ImageResize.resImage(im, "image.jpg", 120, 120);
    if (hd) {
    ImageResize.resImage(im, "image.jpg", 350, 350);
    } else {
    ImageResize.resImage(im, "image.jpg", 100, 100);
    }
    BufferedImage image = ImageIO.read(new File("image.jpg"));
    writeArt(image);
    }
    @@ -118,23 +126,23 @@ private static void writeReversedArt(BufferedImage image) throws FileNotFoundExc
    for (int j = 0; j < length; j++) {
    int val = arr[i][j];
    if (val >= 14913088) {
    sb.append("@@");
    sb.append(" ");
    } else if (val >= 13048952) {
    sb.append("##");
    sb.append("==");
    } else if (val >= 11184816) {
    sb.append("$$");
    sb.append("??");
    } else if (val >= 9320680) {
    sb.append("&&");
    sb.append("||");
    } else if (val >= 7456541) {
    sb.append("**");
    } else if (val >= 5592406) {
    sb.append("||");
    sb.append("&&");
    } else if (val >= 3728271) {
    sb.append("??");
    sb.append("$$");
    } else if (val >= 1864136) {
    sb.append("==");
    sb.append("##");
    } else {
    sb.append(" ");
    sb.append("@@");
    }
    }
    sb.append("\n");
  2. Geek8080 created this gist Jun 29, 2020.
    206 changes: 206 additions & 0 deletions ASCIIArt.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,206 @@
    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.awt.*;
    import java.awt.image.*;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.PrintStream;
    import javax.imageio.ImageIO;

    public class ASCIIArt {
    public static void main(String[] args) throws Exception {
    BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    System.out.println("*************************************");
    System.out.println("* Select an Option: *");
    System.out.println("* *");
    System.out.println("* [1]. Artify Image *");
    System.out.println("* [2]. Artify text *");
    System.out.println("* *");
    System.out.println("*************************************");

    int i = Integer.parseInt(reader.readLine().trim());

    if (i == 1) {
    System.out.print("Enter File Name including the Extension: ");
    asciiArtifyImage(reader.readLine().trim());
    }

    if (i == 2) {
    System.out.print("Enter Text to be artified: ");
    asciiArtifyText(reader.readLine().trim());
    }
    }

    public static void asciiArtifyText(String text) throws FileNotFoundException {
    BufferedImage image = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2d = image.createGraphics();

    Font font = new Font(Font.MONOSPACED, Font.PLAIN, 24);

    g2d.setFont(font);
    FontMetrics fm = g2d.getFontMetrics();
    int width = fm.stringWidth(text);
    int height = fm.getHeight();
    g2d.dispose();

    image = new BufferedImage(width + ((width) / text.length()), height, BufferedImage.TYPE_INT_ARGB);

    g2d = image.createGraphics();
    g2d.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g2d.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY);
    g2d.setRenderingHint(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_ENABLE);
    g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
    g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
    g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
    g2d.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE);
    g2d.setFont(font);
    fm = g2d.getFontMetrics();
    g2d.setColor(Color.BLACK);
    g2d.drawString(text, 0, fm.getAscent());
    g2d.dispose();

    writeBWArt(image);
    }

    public static void asciiArtifyImage(String fileName) throws Exception {
    File im = new File(fileName);
    ImageResize.resImage(im, "image.jpg", 120, 120);
    BufferedImage image = ImageIO.read(new File("image.jpg"));
    writeArt(image);
    }

    private static void writeArt(BufferedImage image) throws FileNotFoundException {
    int arr[][] = convertToArray(image);
    int width = (arr.length);
    int length = (arr[0].length);

    StringBuffer sb = new StringBuffer();

    for (int i = 0; i < width; i++) {
    for (int j = 0; j < length; j++) {
    int val = arr[i][j];
    if (val >= 14913088) {
    sb.append("@@");
    } else if (val >= 13048952) {
    sb.append("##");
    } else if (val >= 11184816) {
    sb.append("$$");
    } else if (val >= 9320680) {
    sb.append("&&");
    } else if (val >= 7456541) {
    sb.append("**");
    } else if (val >= 5592406) {
    sb.append("||");
    } else if (val >= 3728271) {
    sb.append("??");
    } else if (val >= 1864136) {
    sb.append("==");
    } else {
    sb.append(" ");
    }
    }
    sb.append("\n");
    }

    System.setOut(new PrintStream(new File("image.txt")));
    System.out.println(sb.toString());
    }

    private static void writeReversedArt(BufferedImage image) throws FileNotFoundException {
    int arr[][] = convertToArray(image);
    int width = (arr.length);
    int length = (arr[0].length);

    StringBuffer sb = new StringBuffer();

    for (int i = 0; i < width; i++) {
    for (int j = 0; j < length; j++) {
    int val = arr[i][j];
    if (val >= 14913088) {
    sb.append("@@");
    } else if (val >= 13048952) {
    sb.append("##");
    } else if (val >= 11184816) {
    sb.append("$$");
    } else if (val >= 9320680) {
    sb.append("&&");
    } else if (val >= 7456541) {
    sb.append("**");
    } else if (val >= 5592406) {
    sb.append("||");
    } else if (val >= 3728271) {
    sb.append("??");
    } else if (val >= 1864136) {
    sb.append("==");
    } else {
    sb.append(" ");
    }
    }
    sb.append("\n");
    }

    System.setOut(new PrintStream(new File("image.txt")));
    System.out.println(sb.toString());
    }

    private static void writeBWArt(BufferedImage image) throws FileNotFoundException {
    int arr[][] = convertToArray(image);
    int width = (arr.length);
    int length = (arr[0].length);

    StringBuffer sb = new StringBuffer();

    for (int i = 0; i < width; i++) {
    for (int j = 0; j < length; j++) {
    int val = arr[i][j];
    if (val < 9320676) {
    sb.append(" ");
    } else {
    sb.append("##");
    }
    }
    sb.append("\n");
    }

    System.setOut(new PrintStream(new File("image.txt")));
    System.out.println(sb.toString());
    }

    private static void writeReversedBWArt(BufferedImage image) throws FileNotFoundException {
    int arr[][] = convertToArray(image);
    int width = (arr.length);
    int length = (arr[0].length);

    StringBuffer sb = new StringBuffer();

    for (int i = 0; i < width; i++) {
    for (int j = 0; j < length; j++) {
    int val = arr[i][j];
    if (val > 9320676) {
    sb.append(" ");
    } else {
    sb.append("##");
    }
    }
    sb.append("\n");
    }

    System.setOut(new PrintStream(new File("image.txt")));
    System.out.println(sb.toString());
    }

    private static int[][] convertToArray(BufferedImage image) {
    int width = image.getWidth();
    int height = image.getHeight();
    int[][] result = new int[height][width];

    for (int row = 0; row < height; row++) {
    for (int col = 0; col < width; col++) {
    result[row][col] = image.getRGB(col, row) * (-1);
    }
    }

    return result;
    }
    }