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:21] – [Step 1. Read your image and print its height and width in pixels] frchrisascii_art [2023/03/25 14:30] – [Step 3. Convert the RGB tuples of your pixels into single brightness numbers] frchris
Line 33: Line 33:
                    """"                                               """"                           
 </code> </code>
 +
 +This Lab is based on one of  [[https://robertheaton.com/2018/06/12/programming-projects-for-advanced-beginners-ascii-art/|Robert Heaton's Advanced Beginners Projects]].  I have made starter code for Java Students about to take the AP Computer Science A Exam.  You need to know somehting about 2D Arrays and String Objects.
 +
 ==== Step 0. Choose an image ==== ==== Step 0. Choose an image ====
  
Line 126: Line 129:
     try     try
     {     {
-      // the line that reads the image file 
       this.fileName = fileName;       this.fileName = fileName;
       image = ImageIO.read(new File(fileName));       image = ImageIO.read(new File(fileName));
Line 159: Line 161:
   private BufferedImage image;   private BufferedImage image;
   private String fileName;   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)   public Step2(String fileName)
   {   {
Line 167: Line 172:
       image = ImageIO.read(new File(fileName));       image = ImageIO.read(new File(fileName));
       System.out.println(fileName + " read ");       System.out.println(fileName + " read ");
-     +      width = image.getWidth(); 
-      System.out.println("Demensions of " + fileName + ": "); +      height = image.getHeight(); 
-      // Print the width and height of the image using a BufferedImage method.  +      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] ); 
 +      
            
     }      } 
Line 177: Line 190:
     }     }
   }   }
 +  public Color[][] getPx() { return px;}
   public static void main(String[] args)   public static void main(String[] args)
   {   {
Line 185: Line 198:
 } }
 </code> </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  ====
 +
 +Now that you have a 2D Array of ''Color'', you can use the ''Color'' methods of ''getRed()'', ''getGreen()'', and ''getBlue()'' to assign a single brightness number.   There are many different ways to map RGB values to brightness, and each produces a slightly different style of transformed image. Think of them like Instagram filters. Some examples:
 +  - Average: average the R, G and B values - ''(R + G + B) / 3''
 +  - Lightness: average the maximum and minimum values out of R, G and B - ''max(R, G, B) + min(R, G, B) / 2''
 +  - Luminosity: take a weighted average of the R, G and B values to account for human perception - ''0.21 R + 0.72 G + 0.07 B''
 +  - See [[https://stackoverflow.com/questions/596216/formula-to-determine-brightness-of-rgb-color|this stackoverflow thread]] for more detail
 +
 ==== Step 4. Convert brightness numbers to ASCII characters  ==== ==== Step 4. Convert brightness numbers to ASCII characters  ====
 ==== Step 5. What if it looks your image looks squashed? ==== ==== Step 5. What if it looks your image looks squashed? ====
  
  
-[[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