Zanpakuto Pro No.-37 ~Displaying images on the console in python~
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
- Resize (to fit in one line)
- Convert to character every pixel
- Show in Console
It comes to.
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): s = "" for j in range(resized.shape): 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!