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/24 11:04] – [Step 0. Choose an image] frchrisascii_art [2023/03/25 12:22] – [Step 1. Read your image and print its height and width in pixels] frchris
Line 36: Line 36:
  
 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.  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.
 +<code PickPicture.java>
 +import java.awt.Color;
 +import java.awt.Font;
 +import java.awt.Graphics;
 +import javax.swing.JFrame;
 +import javax.swing.JPanel;
 +import java.awt.image.BufferedImage;
 +import java.io.File;
 +import java.io.IOException;
 +import javax.imageio.ImageIO;
 +import java.awt.*;
 +/**
 +   PickPicture is Step 0 of the ASCII Art Lab
 +   @author Chris Thiel, OFMCap
 +   @version 25 Mar 2023
 + */
 +public class PickPicture extends JPanel 
 +{
 +    
 +    private String fileName;
 +    private BufferedImage source;
 +    private Font titleFont, regularFont;
 +
 +    public PickPicture(String fileName)
 +    {
 +        titleFont = new Font("Roman", Font.BOLD, 32);
 +        regularFont = new Font("Helvetica", Font.PLAIN, 24);
 +        try
 +        {
 +            // the line that reads the image file
 +            this.fileName = fileName;
 +            this.source = ImageIO.read(new File(fileName));           
 +            System.out.println(fileName + " read ");
 +            System.out.println(source.getWidth() + " by " + source.getHeight());
 +                               
 +        } 
 +        catch (IOException e)
 +        {
 +            System.out.println("Whoa... something's not right:\n"+
 +                  "Make sure the file is \n 1. In the project folder,\n " +
 +                  "2. The file name is perfect-including capital letters\n " +
 +                  "3. The extension is right-like .png or .jpg\n " + e);
 +        }
 +
 +    }
 +    public int getWidth() {return source.getWidth();}
 +    public int getHeight() {return source.getHeight();}
 +    
 +    public void paintComponent(Graphics g){
 +        super.paintComponent(g);
 +        g.setColor(Color.WHITE);
 +        g.fillRect(0, 0, getWidth(),getHeight());
 +        g.drawImage(source, 0, 0, null);
 +        g.setColor(Color.BLACK);// Use WHITE if the image is dark
 +        g.setFont(titleFont);
 +        g.drawString(fileName, 20, 40);
 +        g.setFont(regularFont);
 +        g.drawString ( getWidth() + " by " + getHeight(), 20, 60);
 +        
 +        
 +    }
 +    
 +    public static void main(String[] args) {
 +        PickPicture pic= new PickPicture("myPicture.jpg"); // change this to the picute you picked
 +        JFrame window = new JFrame("Pick Picture");
 +        window.setSize(pic.getWidth(), pic.getHeight());
 +        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 +        window.getContentPane().add(pic);
 +        window.setVisible(true);
 +
 +    }
 +
 +}
 +</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;
 +  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 ");
 +    
 +      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 Step2("My_Picture.jpg");
 +  }
 +
 +}
 +</code>
 ==== 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  ====
ascii_art.txt · Last modified: 2023/03/27 10:46 by frchris

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki