This program converts an image into ASCII art. It takes an input image file and generates a corresponding ASCII representation, which can be saved to an output text file.
python ascii_converter.py --file <input_image_file> [--scale <scale_factor>] [--out <output_file>] [--cols <num_columns>] [--morelevels]
--file
: (Required) Specifies the input image file.--scale
: (Optional) Specifies the scale factor for the output ASCII art. Default value is 0.43, which suits a Courier font.--out
: (Optional) Specifies the output file name. Default isout.txt
.--cols
: (Optional) Specifies the number of columns for the ASCII art. Default is 80.--morelevels
: (Optional) Adds more levels of gray for better representation. If provided, it uses 70 levels of gray; otherwise, it uses 10 levels of gray.
- Python 3.x
- PIL (Python Imaging Library)
- Open the input image file and convert it to grayscale.
- Determine the dimensions of the image and calculate the dimensions of each tile based on the specified number of columns and scale factor.
- Iterate through each tile of the image, calculate the average luminance, and map it to an ASCII character based on the grayscale level.
- Generate the ASCII representation of the image as a list of character strings.
- Write the ASCII art to the output file.
To convert an image named input.jpg
to ASCII art with 100 columns and a scale factor of 0.5, you can run:
python ascii_converter.py --file input.jpg --cols 100 --scale 0.5
- The grayscale level values used for mapping luminance to ASCII characters are sourced from Paul Bourke's website.
- This code was written for educational purposes and inspired by various ASCII art tutorials and resources available online.