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 12:01] – [Step 0. Choose an image] frchrisascii_art [2023/03/25 14:21] – [Step 5. What if it looks your image looks squashed?] frchris
Line 111: Line 111:
 </code> </code>
 ==== Step 1. Read your image and print its height and width in pixels ==== ==== Step 1. Read your image and print its height and width in pixels ====
 +
 +<code Step1.java>
 +import java.awt.image.BufferedImage;
 +import java.io.File;
 +import java.io.IOException;
 +import javax.imageio.ImageIO;
 +
 +public class Step1 
 +{
 +  private BufferedImage image;
 +  private String fileName;
 +  public Step1(String fileName)
 +  {
 +    try
 +    {
 +      this.fileName = fileName;
 +      image = ImageIO.read(new File(fileName));
 +      System.out.println(fileName + " read ");
 +    
 +      System.out.println("Demensions of " + fileName + ": ");
 +      // Print the width and height of the image using a BufferedImage method.  
 +     
 +    } 
 +    catch (IOException e)
 +    {
 +      System.out.println("Whoa... something's not right:\n"+e);
 +    }
 +  }
 +
 +  public static void main(String[] args)
 +  {
 +    new Step1("My_Picture.jpg");
 +  }
 +
 +}
 +</code>
 ==== Step 2. Load your image’s pixel data into a 2-dimensional array ==== ==== Step 2. Load your image’s pixel data into a 2-dimensional array ====
 +<code Step2.java>
 +import java.awt.image.BufferedImage;
 +import java.io.File;
 +import java.io.IOException;
 +import javax.imageio.ImageIO;
 +
 +public class Step2
 +{
 +  private BufferedImage image;
 +  private String fileName;
 +  private int width,height;
 +  private int[] rgb; // for 8bit RGB packed in an int array
 +  private Color[][] px; 
 +  public Step2(String fileName)
 +  {
 +    try
 +    {
 +      // the line that reads the image file
 +      this.fileName = fileName;
 +      image = ImageIO.read(new File(fileName));
 +      System.out.println(fileName + " read ");
 +      width = image.getWidth();
 +      height = image.getHeight();
 +      System.out.println("Demensions of " + fileName + ": " + width + " by " + height);
 +      rgb = image.getRGB(0,0, width, height, null, 0, width);
 +      System.out.println(rgb.length);
 +      // rgb is a 1D array, with the rows listed one after the other.
 +      ///to convert to a 2D array use something like this:
 +      // grid (x,y) = rgb[ y*width + x];
 +      // Make a new 2D array of color name px, and fill it from the  1D rgb array
 +      // for example, px[0][0] = new Color ( rgb[0] );
 +      
 +     
 +    } 
 +    catch (IOException e)
 +    {
 +      System.out.println("Whoa... something's not right:\n"+e);
 +    }
 +  }
 +  public Color[][] getPx() { return px;}
 +  public static void main(String[] args)
 +  {
 +    new Step2("My_Picture.jpg");
 +  }
 +
 +}
 +</code>
 + You can see what your new 2D Array of Color looks like with {{ ::step2tester.java |Step2Tester.java}}
 +
 ==== Step 3. Convert the RGB tuples of your pixels into single brightness numbers  ==== ==== Step 3. Convert the RGB tuples of your pixels into single brightness numbers  ====
 ==== Step 4. Convert brightness numbers to ASCII characters  ==== ==== Step 4. Convert brightness numbers to ASCII characters  ====
Line 117: Line 202:
  
  
-[[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