Print images to console using Python

Hello everyone, hope you all are doing good. I would like to show you how to print images onto the console using Python. So, let’s get started.

Before getting started, make sure to have pip and python installed in your system

Getting start…


This content originally appeared on DEV Community and was authored by Pranav Baburaj

Hello everyone, hope you all are doing good. I would like to show you how to print images onto the console using Python. So, let's get started.

Before getting started, make sure to have pip and python installed in your system

Getting started

Install required packages

pip install pillow colr

Now, you have setup the development environment. Let's get into the code

First, import the necessary libraries. The script uses the pillow library for Image processing

from PIL import Image
from colr import Colr

# Ask the user for image filename
image_path = input("Image:")

In this example, I am prompting the user to enter the filename for the image and collect it in the image_path variable.

Next, open the image

image = Image.open(image_path)

Now that we have opened the image. we need to read the image and get the colors. For that, use

pixel_values = image.getdata()

The image.getdata() function returns a list of RGB or RGBA values.

Now, lets print the image

width, height = image.size
for index, character in enumerate(pixel_values):
    if not isinstance(character, (tuple, list)):
        continue
    r, g, b = character[:-1]
    if index % width == 0:
        print("")
    print(Colr().rgb(r, g, b, "\u2584"), end="")

Wait, let me explain.

width, height = image.size

This image.size property is a tuple containing the width and the height of the image.

for index, character in enumerate(pixel_values):
    if not isinstance(character, (tuple, list)):
        continue

We loop through the list of rgb colors and make sure that all the values are either in the form of tuple or list.

r, g, b = character[:-1]

This line unpacks the tuple into variables for red(r), green(g) and blue(b)

print(Colr().rgb(r, g, b, "\u2584"), end="")

This line prints a pixel onto the screen.

I have created a python library for printing images onto the console. Check out the GitHub repository

GitHub logo pranavbaburaj / img

A python library to display images in the terminal

Img

Display Images in your terminal with python

Installation

The package can be installed via pip

pip install terminal-img

Quick Start

The library is really simple to get started with. Here's is an example of how you display an image

from image import DrawImage
image = DrawImage("image.png")

You can also use a url if you dont have the file locally stored

image = DrawImage.from_url("url")

Methods

image.DrawImage

  • filename: The name of the file containing the image
  • size(Optional[Tuple]) : The size of the image to be displayed. Default: 24, 24
  • draw: Whether to draw on creating an instance or wait for the user to call the function

image.DrawImage.from_url

  • url : The url of the image
  • size(Optional[Tuple]) : The size of the image to be displayed. Default: 24, 24
  • draw: Whether to draw on…

Please let me know what you thing. Hope you learned something new today. Thank you for reading :)


This content originally appeared on DEV Community and was authored by Pranav Baburaj


Print Share Comment Cite Upload Translate Updates
APA

Pranav Baburaj | Sciencx (2021-09-29T17:40:12+00:00) Print images to console using Python. Retrieved from https://www.scien.cx/2021/09/29/print-images-to-console-using-python/

MLA
" » Print images to console using Python." Pranav Baburaj | Sciencx - Wednesday September 29, 2021, https://www.scien.cx/2021/09/29/print-images-to-console-using-python/
HARVARD
Pranav Baburaj | Sciencx Wednesday September 29, 2021 » Print images to console using Python., viewed ,<https://www.scien.cx/2021/09/29/print-images-to-console-using-python/>
VANCOUVER
Pranav Baburaj | Sciencx - » Print images to console using Python. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/09/29/print-images-to-console-using-python/
CHICAGO
" » Print images to console using Python." Pranav Baburaj | Sciencx - Accessed . https://www.scien.cx/2021/09/29/print-images-to-console-using-python/
IEEE
" » Print images to console using Python." Pranav Baburaj | Sciencx [Online]. Available: https://www.scien.cx/2021/09/29/print-images-to-console-using-python/. [Accessed: ]
rf:citation
» Print images to console using Python | Pranav Baburaj | Sciencx | https://www.scien.cx/2021/09/29/print-images-to-console-using-python/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.