Saturday, November 16, 2024
Google search engine
HomeLanguagesDisplay Images on Terminal using Python

Display Images on Terminal using Python

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: 

RELATED ARTICLES

Most Popular

Recent Comments