Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Display Images on Terminal using Python
Displaying images on a terminal window can be a useful and fun feature to add to your Python programs. It can be used to create ASCII art, display diagrams or charts, or simply show images in a terminal-based interface. In this article, we'll explore how to display images on a terminal using Python.
First, let's understand the basic concept of displaying images on a terminal. A terminal window is essentially a text-based interface that displays characters on the screen. Therefore, to display an image, we need to convert it into characters that can be displayed on a terminal.
Method 1: Using art Library
The art library is a popular Python library for creating ASCII art from text and images. It provides simple functions to convert images into terminal-displayable ASCII representations.
Installation
pip install art
Example
Here's how to convert an image to ASCII art using the art library ?
from art import text2art
# Create ASCII art from text
result = text2art("PYTHON")
print(result)
____ _ _ _____ _ _ ___ _ _ | _ \| | | |_ _| | | |/ _ \| \ | | | |_) | |_| | | | | |_| | | | | \| | | __/| _ | | | | _ | |_| | |\ | |_| |_| |_| |_| |_| |_|\___/|_| \_|
Method 2: Using Pillow for Image Conversion
For actual image files, we can use the Pillow library to load and process images, then convert them to ASCII characters based on pixel brightness.
Installation
pip install pillow
Example
Here's a complete example that converts an image to ASCII art ?
from PIL import Image
def image_to_ascii(image_path, width=80):
# ASCII characters from darkest to lightest
ascii_chars = "@%#*+=-:. "
# Open and process the image
img = Image.open(image_path)
# Convert to grayscale
img = img.convert('L')
# Calculate new height to maintain aspect ratio
aspect_ratio = img.height / img.width
height = int(aspect_ratio * width * 0.55) # 0.55 to adjust for character height
# Resize image
img = img.resize((width, height))
# Convert pixels to ASCII
ascii_art = ""
for y in range(height):
for x in range(width):
pixel = img.getpixel((x, y))
ascii_art += ascii_chars[pixel // 32] # Map 0-255 to 0-7
ascii_art += "\n"
return ascii_art
# Example usage (you would need an actual image file)
# ascii_result = image_to_ascii("sample.jpg", width=60)
# print(ascii_result)
# Demo with a simple pattern
demo_pattern = """
@@@@@@@@@@
@ @
@ DEMO @
@ @
@@@@@@@@@@
"""
print("Demo ASCII pattern:")
print(demo_pattern)
Demo ASCII pattern: @@@@@@@@@@ @ @ @ DEMO @ @ @ @@@@@@@@@@
Method 3: Using opencv-python for Advanced Processing
OpenCV provides more advanced image processing capabilities for terminal display ?
pip install opencv-python
Example
import cv2
import numpy as np
def create_ascii_from_array(arr, width=40):
"""Convert a numpy array to ASCII representation"""
ascii_chars = " .:-=+*#%@"
# Normalize array to 0-9 range
normalized = ((arr - arr.min()) / (arr.max() - arr.min()) * 9).astype(int)
ascii_art = ""
for row in normalized:
for pixel in row:
ascii_art += ascii_chars[pixel]
ascii_art += "\n"
return ascii_art
# Create a sample gradient array
gradient = np.linspace(0, 255, 400).reshape(20, 20)
ascii_gradient = create_ascii_from_array(gradient)
print("ASCII Gradient:")
print(ascii_gradient)
ASCII Gradient:
.....-----
----======+++***###
###%%%@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@
Comparison
| Method | Best For | Complexity | File Support |
|---|---|---|---|
art |
Text to ASCII art | Simple | Text only |
Pillow |
Image files to ASCII | Medium | Most image formats |
OpenCV |
Advanced processing | Complex | Images + video |
Key Points
- ASCII characters represent different brightness levels
- Image aspect ratio should be maintained during conversion
- Terminal character height affects the final appearance
- Grayscale conversion simplifies the ASCII mapping process
Conclusion
Displaying images on a terminal using Python can be achieved through multiple approaches. Use the art library for simple text-based ASCII art, Pillow for converting image files, or OpenCV for advanced image processing and terminal display capabilities.
---