DEV Community

natamacm
natamacm

Posted on

Terminal image with Python

You can display an image in terminal. Using Python you can use the module timg.

Setup

timg is available as a package on PyPI. You can install it with pip:

$ pip install timg

Now you can use the timg command in your terminal.

usage

To display an image from code:

import timg

obj = timg.Renderer()                                                                                               
obj.load_image_from_file("test.png")                                                                                
obj.resize(100,40)
obj.render(timg.ASCIIMethod)

That turns the image into ascii art.

To run it as command line program:

    usage: timg [-h] [-V] [-i] [-m METHOD] [-r N] [-s W] [filename]

    print an image in terminal


    positional arguments:
      filename              filename of an image

    optional arguments:
      -h, --help            show this help message and exit
      -V, --version         print version and exit
      -i, --invert-background
                            invert grayscale in ASCII mode
      -m METHOD, --method METHOD
                            name of a rendering method (use `-m list` to list
                            available methods, the default is a24h)
      -r N, --reduce-colors N

                            reduce color palette of an input image (1-256)
      -s W, --size W        width of an image

Top comments (2)

Collapse
 
owijo12 profile image
owijo12 • Edited

how can i use this in a python program ???, (no asci art)
every time i load i get nothing code below ↓

import timg
def save():
    print()

    obj = timg.Renderer()                                                                                               
    obj.load_image_from_file("moonblight.jpg")                                                                                
    obj.resize(100,40)
    obj.render
    city_options = """                       
                         | A) enter the city                                                                     |
                         | B) go look for food                                                                   |
                         | C) go  back into your bunker and die from stupidity                                   |
                         | D) screem for help                                                                    |
                         |_______________________________________________________________________________________|"""

    print(city_options)
    x = input()






save()
Enter fullscreen mode Exit fullscreen mode
Collapse
 
sdparquinn profile image
Sean Parchem

Hate to bother but was also wondering how to run this example code you gave but without it applying any styles like ASCII to it.