簡體   English   中英

使用 PIL 在終端中顯示圖像(png)

[英]Using PIL to show image (png) in terminal

環境:

Python 3.7.2 Mac 操作系統 10.14.3

我試圖找到一種在終端應用程序中顯示圖像(jpg/png)的方法。

我在這里找到了 jpg 圖像的有效解決方案:

使用 Python 在 Linux 終端中顯示圖像

使用以下代碼:

import numpy as np
from PIL import Image

def get_ansi_color_code(r, g, b):
    if r == g and g == b:
        if r < 8:
            return 16
        if r > 248:
            return 231
        return round(((r - 8) / 247) * 24) + 232
    return 16 + (36 * round(r / 255 * 5)) + (6 * round(g / 255 * 5)) + round(b / 255 * 5)

def get_color(r, g, b):
    return "\x1b[48;5;{}m \x1b[0m".format(int(get_ansi_color_code(r,g,b)))

def show_image(img_path):
    try:
        img = Image.open(img_path)
    except FileNotFoundError:
        exit('Image not found.')
    h = 100
    w = int((img.width / img.height) * h)
    img = img.resize((w, h), Image.ANTIALIAS)
    img_arr = np.asarray(img)

    for x in range(0, h):
        for y in range(0, w):
            pix = img_arr[x][y]
            print(get_color(pix[0], pix[1], pix[2]), sep='', end='')
        print()

if __name__ == '__main__':
    show_image(sys.argv[1])

問題是當我嘗試將此代碼用於 png 文件時,出現錯誤:

Traceback (most recent call last):
  File "img-viewer.py", line 62, in <module>
    show_image(sys.argv[1])
  File "img-viewer.py", line 40, in show_image
    print(get_color(pix[0], pix[1], pix[2]), sep='', end='')
IndexError: invalid index to scalar variable.

似乎在處理 jpg 文件時, pix是一個元組,而對於 png 文件, pix是一個 int 值。

任何建議將不勝感激,謝謝:)

您的圖像可能是灰度的或調色的。 無論哪種方式都只會有 1 個通道,而不是 3 個。所以更改此行

img = Image.open(img_path)

img = Image.open(img_path).convert('RGB')

所以你得到了你期望的 3 個頻道,而且一切都很好。


我注意到您的調整大小代碼試圖在調整后的圖像中保持相同的縱橫比,這都非常值得稱贊,但是……終端上的像素實際上並不是方形的! 如果您近距離觀察光標,它的高度大約是寬度的 2 倍,因此我更改了調整大小代碼行以允許這樣做:

w = int((img.width / img.height) * h) * 2

關鍵詞:PIL、枕頭、終端、控制台、ANSI、轉義序列、圖形、ASCII 藝術、圖像、圖像處理、Python

只需將重新着色的 img 分成如下七組,

front     |     back        |   color
------------------------------------------
30        |        40       |    black
31        |        41       |    red
32        |        42       |    green
33        |        43       |    yello
34        |        44       |    blue
35        |        45       |    purple
36        |        46       |    violet
37        |        47       |    white
-------------------------------------------

還有代碼,

from PIL import Image  
import numpy as np  
import sys
import os

image = Image.open(sys.argv[1])   
img = np.array(image)

x=5 #step
y=5 #step
img1=img[0:img.shape[0]:x,0:img.shape[1]:y]
#img1=img1 // 37 #resize

for i in range(img.shape[0]//x):
    str = "echo "
    for j in range(img.shape[1]//y):
        if img1[i, j] == 0:
            str=str+'\x1b[41;31m  '
        elif img1[i,j]==1:
            str=str+'\x1b[42;31m  '
        elif img1[i,j]==2:
            str=str+'\x1b[43;31m  '
        elif img1[i,j]==3:
            str=str+'\x1b[44;31m  '
        elif img1[i,j]==4:
            str=str+'\x1b[45;31m  '
        elif img1[i,j]==5:
            str=str+'\x1b[46;31m  '
        elif img1[i, j] == 6:
            str = str + '\x1b[47;31m ' 
    str = str + '\x1b[0m'
    os.system(str)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM