Skip to the content.
Unit 8 Lesson 8.1 Lesson 8.2 ASCII image Homework

Unit 8 - Image Converter to ASCII

By Torin

Image Converter to ASCII

Introduction

In this project, we will be creating a program that converts an image to ASCII art. ASCII art is a graphic design technique that uses printable characters from the ASCII standard to create images and designs. In this project, we will be using Java to convert an image to ASCII art with for loops and other methods.

import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;

public class ImageToAscii {
    private static final String ASCII_CHARS = "@%#*+=-:. ";

    public static void main(String[] args) {
        try {
            BufferedImage image = ImageIO.read(new File("/home/adityasamavedam/csa/adityacsastudent/images/BrawlStars.png"));
            String asciiArt = convertToAscii(image);
            System.out.println(asciiArt);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static String convertToAscii(BufferedImage image) {
        int width = 100; // Resize width
        double aspectRatio = 0.35; // Adjust this value to correct the vertical stretching
        int height = (int) (image.getHeight() * (100.0 / image.getWidth()) * aspectRatio); // Maintain aspect ratio with adjustment
        BufferedImage resizedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        resizedImage.getGraphics().drawImage(image, 0, 0, width, height, null);

        StringBuilder asciiArt = new StringBuilder();
        for (int y = 0; y < height; y++) {
            for (int x = 0; x < width; x++) {
                int pixel = resizedImage.getRGB(x, y);
                int grayValue = (int) (((pixel & 0x00FF0000) >> 16) * 0.299 + ((pixel & 0x0000FF00) >> 8) * 0.587 + (pixel & 0x000000FF) * 0.114);
                int charIndex = (int) ((grayValue / 255.0) * (ASCII_CHARS.length() - 1));
                asciiArt.append(ASCII_CHARS.charAt(charIndex));
            }
            asciiArt.append("\n");
        }
        return asciiArt.toString();
    }
}

// Run the main method
ImageToAscii.main(null);
                            .-+@@@@@:@@@@@@@:@@@@@#@:.:-:%@@@#@@@@@@@@%                             
                            @       @=  -:  .=      @    *   ==   %   @                             
                            *@      .@      @@   +   @           @@  =@                             
                %@@@@@@**#%@@@      .+  @   @   @@@   @    -@    @@     @--@@@@@@@@-                
             @@**************@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@#************#@.            
            +@**************@*************%***********@@@#***********@@*************#@              
            :@**********#@@@**************@@***********@@*********************%@@@@@@               
              @%*************@@@*****#@@@@@******@******@%****#@@******@***********%@%              
              *@@@@@@@%*******@@#*****@@@@%**************#************@@@@@@@%*******@#             
              @#**************@@%*****@@@@***************************@@**************@=             
             *@*************@@@@@*****@@@******@@#@@#**********@*******************@@-              
              @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@  .::=@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@                 
                -@@@@@@@@@@@@@+  @  *@@@%-    @@@@@@@:::::%@@@@  @@@@@@@@@@@@@@@@                   
                                -:   .@::: @-    ------@*---=%   -@                                 
                                 @@   +=::@----%@-+%----@%--@   :%                                  
                                 @    =+::@-+@@@@--@@@#-@+--@    @=                                 
                                 -@   @:::-@@@-------%@@:---=%  %:                                  
                                    %@@@#-----:@@@@@--++++%@@@@                                     
                                      :@@@@@@@@---++*@@@@@@@@                                       
                                              @@@%@@@                                               
                                                 @