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
Next revisionBoth sides next revision
ascii_art [2023/03/25 15:37] – [Step 2. Load your image’s pixel data into a 2-dimensional array] frchrisascii_art [2023/03/26 11:21] – [Step 4. Convert brightness numbers to ASCII characters] frchris
Line 158: Line 158:
 import javax.imageio.ImageIO; import javax.imageio.ImageIO;
 /** /**
- Test your Step2 code with this. It reads your 2D array of Color + Start Step2 with this code 
- * and draws it.+ * The image will return a single line of ints that represent color 
 + * and you need to make a 2D Array of Color from it.  
 + *
  * @author Chris Thiel, OFMCap  * @author Chris Thiel, OFMCap
  * @version 25 Mar 2023  * @version 25 Mar 2023
Line 302: Line 304:
  
     }     }
 +    public int[][] getBrightness() 
 +    { 
 +        return brightness; 
 +    }
     public static void main(String[] args) {     public static void main(String[] args) {
-        Step3 pic= new Step3("StBernardSm.jpg"); // change this to the picture you picked +        Step3 pic= new Step3("MyPicture.jpg"); // change this to the picture you picked 
-        JFrame window = new JFrame("Step 2 Tester");+        JFrame window = new JFrame("Step 3");
         window.setSize(pic.getWidth(), pic.getHeight());         window.setSize(pic.getWidth(), pic.getHeight());
         window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);         window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Line 315: Line 320:
 </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.
  
 +<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 ====
  
-==== Step 5. What if it looks your image looks squashed? ==== 
  
 +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. 
  
 [[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.txt · Last modified: 2023/03/27 10:46 by frchris

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki