User Tools

Site Tools


ascii_art

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
ascii_art [2023/03/25 15:47] – [Step 3. Convert the RGB tuples of your pixels into single brightness numbers] frchrisascii_art [2023/03/27 10:46] (current) frchris
Line 1: Line 1:
 ====== ASCII Art ====== ====== ASCII Art ======
 +[[https://upload.wikimedia.org/wikipedia/commons/c/cf/USASCII_code_chart.png|ASCII]] Art is made using nothing but the letters found on the known that come from the [[https://en.wikipedia.org/wiki/ASCII|American Standard Code for Information Interchange (ASCII)]].  You can see an example below, found at [[https://www.asciiart.eu|www.asciiArt.eu]].  
  
 <code> <code>
Line 38: Line 39:
 ==== Step 0. Choose an image ==== ==== Step 0. Choose an image ====
  
-Choose the first image that you want to convert into ASCII art. It’s good to start with an image around 640x480.  It can be in color, a photo, or a drawing.  Light pixels will be represented by small characters like a dot (.), which leave lots of the background exposed. On the other extreme, dense characters like $ are used to cover up the background.+Choose the first image that you want to convert into ASCII art. It’s good to start with an image around 640x480 or smaller (Of course, you can shrink and crop an image of any size down to this).  It can be in color, a photo, or a drawing. I think it is easier to get nice results from a cartoon or logo or a high contrast photo or drawing. More subtle photos look good in a very large array of characters, so you would need to miniaturize or print on a large format printer to get a good effect. 
 + 
 +Light pixels will be represented by small characters like a dot (.), which leave lots of the background exposed. On the other extreme, dense characters like $ are used to cover up the background. You can easily invert this later.  
 <code PickPicture.java> <code PickPicture.java>
 import java.awt.Color; import java.awt.Color;
Line 320: Line 324:
 </code> </code>
 ==== Step 4. Convert brightness numbers to ASCII characters  ==== ==== Step 4. Convert brightness numbers to ASCII characters  ====
-You can experiment with different ways to map brightnesses to characters, but a good place to start is the string below. The characters in it are ordered from thinnest to boldest, which means lightest to darkest.+You can experiment with different ways to map brightnesses to characters, but a good place to start is the string in the starter code. The characters in it are ordered from thinnest to boldest, which means lightest to darkest. To get started you may wish to use {{ ::asciiartchars.txt |this arrangement}} 
 + 
 +<code Step4.java> 
 +import java.awt.Color; 
 +import java.awt.Font; 
 +import java.awt.Graphics; 
 +import javax.swing.JFrame; 
 +import javax.swing.JPanel; 
 +import java.awt.*; 
 +import java.nio.file.Files; 
 +import java.io.IOException; 
 +import java.nio.file.Paths; 
 + 
 +/** 
 +   This is Step4 of the ASCII Art Lab: 
 +   Convert your lightness values into 
 +   ASCII Characters. 
 +    
 +   This code requires a functional Step3 
 +    
 +   @author Chris Thiel, OFMCap 
 +   @version 25 Mar 2023 
 + */ 
 +public class Step4  
 +
 + 
 +    private String fileName; 
 +    private Font titleFont, regularFont; 
 +    
 +    private int[][] brightness; 
 +    private char[][] ascii; 
 +     
 +    
 +    public Step4(String fileName) 
 +    { 
 +         
 +        this.fileName = fileName; 
 +        this.brightness = new Step3(fileName).getBrightness(); 
 +        titleFont = new Font("Roman", Font.BOLD, 32); 
 +        regularFont = new Font("Helvetica", Font.PLAIN, 24); 
 +        //Make a 2D int array that matched the dimentions of the Brightness array 
 +         
 +        ascii = new char[brightness.length][brightness[0].length]; 
 +         
 +        //Fill the asciii array with your choice of conversion method 
 +         
 +        // your code here 
 + 
 +    } 
 +    /** 
 +      asciiChar will take your lightValue and  
 +      convert it to a  proportion of 255. 
 +      (To invert the dark and light, the the proportion is 1.0 - proportion.) 
 +       
 +      The proportion is used to select the charactor from the character spectrum 
 +        
 +     */ 
 +    public char asciiChar( int lightValue) 
 +    { 
 +        String chars = ""; // list the chars from one extreame to the other 
 +        int max = chars.length(); 
 +       // your code here 
 +    } 
 +    public int getWidth() { 
 +        return brightness.length;// width is max x i.e, the max col 
 +    } 
 + 
 +    public int getHeight() { 
 +        return brightness[0].length; // height is the max y, i.ie max row 
 +    } 
 +    /** 
 +     * The ascii array is constructed with a newline "\n" between the rows 
 +     */ 
 +    public String toString(){ 
 +       // your code here 
 +    } 
 +    public void writeToFile() 
 +    { 
 +        String name = fileName.substring(0, fileName.indexOf(".")) + ".txt";        
 +        try { 
 +           Files.write(Paths.get(name), toString().getBytes()); 
 +        } catch (IOException e){ 
 +             e.printStackTrace(); 
 +        }  
 +    } 
 +    public static void main(String[] args) { 
 +        System.out.println("Converting Pricture to ASCII Art"); 
 +        Step4 pic= new Step4("MyPicture.jpg"); // change this to the picture you want 
 +        System.out.println(pic); 
 +        pic.writeToFile(); 
 +    } 
 + 
 +
 +</code> 
 +==== Step 5. What if it looks your image looks stretched or too big ==== 
  
 +Characters tend to be three times taller than wide, so you could replace each character with three. If that is too large, you could make one character represent the average of three columns. 
  
-==== Step 5. What if it looks your image looks squashed? ====+==== Here are some examples ==== 
 +  - {{ ::fry.txt |}} 
 +  - {{ ::stbernard.txt |}}
  
  
 +==== See more from Robert Heaton Here ====
 [[https://robertheaton.com/2018/06/12/programming-projects-for-advanced-beginners-ascii-art/|This is from Robert Heaton's Advanced Beginners Projects]] [[https://robertheaton.com/2018/06/12/programming-projects-for-advanced-beginners-ascii-art/|This is from Robert Heaton's Advanced Beginners Projects]]
ascii_art.1679773641.txt.gz · Last modified: 2023/03/25 15:47 by frchris

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki