Display Images on Terminal using Python
Last Updated :
04 Jul, 2021
In this article, we will discuss how to display images on a terminal using Python. We will be using the climage module for the same. This module has the following features -
- It helps to convert images to its ANSI Escape Codes to be able to convert be printable on Command-Line Interfaces.
- It allows 8/16/256 bit color codings for vivid images.
- It provides ASCII/Unicode support for more detail and adjustable pallets for different terminal themes
Installation
This module does not come built-in with Python. To install this type the below command in the terminal.
pip install climage
After the installation, the next step is to import convert() and to_file() functions, where the former performs the task of conversion and the latter performs the task of conversion and saving to the output file if needed.
Syntax:
convert(filename, is_unicode=False, is_truecolor=False, is_256color=True, is_16color=False, is_8color=False, width=80, palette="default")
Parameters:
filename : Name of image file.
is_unicode : If true, conversion is done in unicode format, otherwise ASCII characters will be used.
is_truecolor : Whether to use RGB colors in generation, if supported by terminal. Defaults False.
is_256color : Whether to use 256 colors encoding. Defaults True.
is_16color : Whether to use 16 colors encoding. Defaults False.
is_8color : Whether to use first 8 System colors. Defaults False.
width : Number of blocks of console to be used. Defaults to 80.
palette : Sets mapping of RGB colors scheme to system colors. Options are : ["default", "xterm", "linuxconsole", "solarized", "rxvt", "tango", "gruvbox", "gruvboxdark"]. Default is "default".
to_file(infile, outfile, is_unicode=False, is_truecolor=False, is_256color=True, is_16color=False, is_8color=False, width=80, palette="default")
Parameters:
infile : The name/path of image file.
outfile : File in which to store ANSI encoded string.
Example 1: Printing on Terminal
Image Used:
Python3
import climage
# converts the image to print in terminal
# inform of ANSI Escape codes
output = climage.convert('banana.png')
# prints output on console.
print(output)
Output :
Example 2: Saving Encoding to file.
Python3
import climage
# saves the converted encoded string
# to banana_ansi file.
output = climage.to_file('banana.png', 'banana_ansi')
Output :
[48;5;15m [48;5;15m [48;5;15m [48;5;15m [48;5;15m [48;5;15m [48;5;15m [48;5;15m [48;5;15m [48;5;15m [48;5;15m [48;5;15m [48;5;15m [48;5;15m [48;5;15m [48;5;15m [48;5;15m [48;5;15m [48;5;15m [48;5;15m [48;5;15m [48;5;15m [48;5;15m [48;5;15m [48;5;15m [48;5;15m [48;5;15m [48;5;15m [48;5;15m [48;5;15m [48;5;15m [48;5;15m [48;5;15m [48;5;15m [48;5;15m [48;5;15m [48;5;15m [48;5;15m [48;5;15m [48;5;15m [0m ...
Example 3: Working using command line
A similar function can also be used to work with the command line using similar constructs, parameters explained in previous part.
Syntax :
climage [-h] [-v] [--unicode | --ascii] [--truecolor | --256color | --16color | --8color] [--palette {default,xterm,linuxconsole,solarized,rxvt,tango,gruvbox,gruvboxdark}] [-w width] [-o outfile] inputfile
Working:
Example 4: Custom examples using command line
Below example show working with customized examples setting different possible parameters.
Example 5: Custom examples using Python code.
Extending to previous part, this section shows how custom parameters can be used from the code to construct different images.
Python3
import climage
# converts the image to print in terminal
# with 8 color encoding and palette tango
output = climage.convert('banana.png', is_8color=True,
palette='tango', is_256color=False)
# prints output on console.
print(output)
Output:
Similar Reads
Python | Display images with PyGame
Pygame is a cross-platform set of Python modules designed for writing video games. It includes computer graphics and sound libraries designed to be used with the Python programming language. Now, itâs up to the imagination or necessity of the developer, what type of game he/she wants to develop usin
2 min read
How to display image using Plotly?
A Plotly is a Python library that is used to design graphs, especially interactive graphs. It can plot various graphs and charts like histogram, barplot, boxplot, spreadplot, and many more. It is mainly used in data analysis as well as financial analysis. plotly is an interactive visualization libra
2 min read
Image Viewer App in Python using Tkinter
Prerequisites: Python GUI â tkinter, Python: Pillow Have you ever wondered to make a Image viewer with the help of Python? Here is a solution to making the Image viewer with the help of Python. We can do this with the help of Tkinter and pillow. We will discuss the module needed and code below. Mod
5 min read
Formatted text in Linux Terminal using Python
This article demonstrates how to print formatted text in Linux terminal using Python programming language. Formatted text(also called styled text or rich text) as opposed to plain text, has styling information like: color (text color, background color), style (bold or italic), and some other special
4 min read
Python | How to make a terminal progress bar using tqdm
Whether youâre installing software, loading a page, or doing a transaction, it always eases your mind whenever you see that small progress bar giving you an estimation of how long the process would take to complete or render. If you have a simple progress bar in your script or code, it looks very pl
5 min read
Using the Cat Command in Python
The cat command is a Linux shell command. It is the shorthand for concatenate. It is placed among the most often used shell commands. It could be used for various purposes such as displaying the content of a file on the terminal, copying the contents of a given file to another given file, and both a
4 min read
Open and Run Python Files in the Terminal
The Linux terminal offers a powerful environment for working with Python files, providing developers with efficient ways to open, edit, and run Python scripts directly from the command line. Open and Run Python Files in the Linux TerminalIn this article, we'll explore various techniques and commands
2 min read
Python | Display text to PyGame window
Pygame is a cross-platform set of Python modules designed for writing video games. It includes computer graphics and sound libraries designed to be used with the Python programming language. Now, itâs up to the imagination or necessity of the developer, what type of game he/she wants to develop usin
6 min read
Print Colors in Python terminal
In this article, we will cover how to print colored text in Python using several methods to output colored text to the terminal in Python. The most common ways to do this are using: Using colorama ModuleUsing termcolor ModuleUsing ANSI Code in PythonMethod 1: Print Color Text using colorama Module
3 min read
Python | os.get_terminal_size() method
OS module in Python provides functions for interacting with the operating system. OS comes under Pythonâs standard utility modules. This module provides a portable way of using operating system dependent functionality. os.get_terminal_size() method in Python is used to query the size of a terminal.
1 min read