Zanpakuto Pro No.-37 ~Displaying images on the console in python~

0

I want to check the image quickly

Usually, when working in the command prompt or terminal, it takes time to check the image. In the case of cv2, you can display the image with the imshow method, but that is also troublesome.

Therefore, this program displays an ASCII art version of the image on the console. The flow of processing is

  • Loading images
  • Grayscale
  • Resize (to fit in one line)
  • Convert to character every pixel
  • Show in Console

It comes to.

ImagePrint.py
import cv2

density = list("MWN$@%#&B89EGA6mK5HRkbYT43V0JL7gpaseyxznocv?jIftr1li*=-~^`':;,. ")
LEN = len(density)

def image_print(image, wid=30):
    gray =  cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    resized = cv2.resize(gray, (int(wid*2),int(wid)))
    for i in range(resized.shape[0]):
        s = ""
        for j in range(resized.shape[1]):
            s += density[(LEN-1)-resized[i][j]//(256//LEN)]
        print(s)


if __name__ == '__main__':
    img = cv2.imread("lenna.png")
    image_print(img, 30)

You can also use ANSI escape sequences to play videos, so be sure to give it a try!

bibliography

  1. I made a program to make images ascii-art in Python and OpenCV
Share:
0
Author by

残りの人生のためにプログラミング勉強してます. あと60年生きます. 【残プロ】 月-金:通常投稿 土:番外編,実用例紹介 日:一週間のまとめ,次週の計画 ※現在,多忙のため停止中

Updated on July 09, 2021