An Interest In:
Web News this Week
- April 24, 2024
- April 23, 2024
- April 22, 2024
- April 21, 2024
- April 20, 2024
- April 19, 2024
- April 18, 2024
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 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 Imagefrom colr import Colr# Ask the user for image filenameimage_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.sizefor 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
pranavbaburaj / img
A python library to display images in the terminal
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 DrawImageimage = 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 imagesize
(Optional[Tuple]
) : The size of the image to be displayed. Default: 24, 24draw
: 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 imagesize
(Optional[Tuple]
) : The size of the image to be displayed. Default: 24, 24draw
: Whether to draw on
Please let me know what you thing. Hope you learned something new today. Thank you for reading :)
Original Link: https://dev.to/pranavbaburaj/print-images-to-console-using-python-23k6
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To