Mohamed Yassine Hemissi

Student • Dreamer • Human

HomeProjectsBlog

© 2026 Mohamed Yassine Hemissi. All rights reserved.

← Back to projects

Image to ASCII Art

Early Work

A Python script that converts images (PNG, JPG, JPEG) into ASCII art. Fully commented and explained chunk by chunk for clarity.

2021·By Mohamed Yassine Hemissi·Updated September 6, 2021
Engineering

Technologies Used

PythonASCII ArtImage ProcessingPIL
View on GitHub →

This project converts images into ASCII art using Python. Below is the chunk-by-chunk explanation of the code with each chunk in its own code block and a short description.

1. Importing required libraries

This chunk imports the PIL library for image manipulation and os.path to check file paths.

from PIL import Image import os.path

2. Mapping grayscale values to ASCII characters

Defines a dictionary that maps ranges of grayscale values to ASCII characters for visualization.

# setting ASCII values based on the gray scale VALUES = { 30:" ", 60 :"*", 100: "%", 150 :"o", 255 :"@" }

3. Defining output image size

Sets the width and height for the resized image to ensure proper ASCII art proportion.

# setting the picture new size width and height W, H = 60, 30

4. Calculating grayscale of a pixel

A function to convert an RGB pixel to a single grayscale value by averaging the channels.

# Calculating the gray scale (summing the rgb values and dividing by 3) def calculate_gray_scale(pixel_rgb_values): pix = 0 for z in range(3): pix = pix + pixel_rgb_values[z] return pix / 3

5. Mapping grayscale to ASCII character

This function takes a grayscale value and returns the corresponding ASCII character from VALUES.

# Output valid ASCII character (comparing to the VALUES dictionary) def get_valid_char(gray_scale): for z in VALUES: if (gray_scale <= z): return VALUES[z]

6. Main function: input and validation

Prompts the user to input a local image path and validates the file type.

def main(): # Getting the picture path and checking validity path = input("insert your full image path here (should be local):") if not os.path.isfile(path) or not path.lower().endswith(('.png', '.jpg', '.jpeg')): return main()

7. Opening and resizing the image

Opens the image with PIL and resizes it to the predefined width and height.

# Opening image and resizing it for better quality myImage = Image.open(path) myImage = myImage.resize((W,H))

8. Extracting pixel data and building output

Loops through each pixel, converts it to grayscale, maps to ASCII, and builds the final output string.

# Declaring the output string output = "\n" # Extracting the pixel values pixel_values = list(myImage.getdata()) # Generating the output ASCII string for y in range(H): for x in range(W): pix = calculate_gray_scale(pixel_values[x + W * y]) output = output + get_valid_char(pix) output = output + "\n"

9. Printing and restarting

Prints the generated ASCII art and restarts the script to allow processing another image.

# Printing out print(output) # Restarting the script main() # Starting the script main()

This chunk-by-chunk explanation shows how the program converts any local image into ASCII art while keeping it readable and reusable.