Python打印字符RGBA

时间:2018-10-30 07:13:43

标签: python colors jupyter

我在打印时遇到问题。 请告诉我是否可以用自己的颜色打印每个字符?我需要用其颜色打印“像素”。当前,我正在使用黑色/白色分配器黑色(rgb的所有三个均为0或a为0) `

from colorama import Fore
import matplotlib.pyplot as plt
import numpy as np
from PIL import Image
class picture:
    def __init__(self, name="untitled image"):
        self.height = 5
        self.width = 5
        self.name = "Untitled"
        self.coords = dict()
        self.name = name
        for x in range(0, self.width):
            for y in range(0, self.height):
                self.coords[tuple((x,y))] = [255, 255, 255, 255]
    def check_dict(self):
        counter = 0
        for coord in self.coords:
            counter+=1
            for pixel in coord:
                if pixel <0 or pixel >255:
                    return 0
        return counter == self.width*self.height
    def fill_with(self, rgba):
        for y in range(0, self.height):
            for x in range(0, self.width):
                self.coords[(x,y)] = list(rgba)
    def fill_black(self):
        self.fill_with([0,0,0, 255])
    def get_matrix(self):
        matrix = []
        for y in range(0, self.height):
            matrix.append([])
            for x in range(0, self.width):
                matrix[y].append(())

        for coord in self.coords:
            matrix[coord[0]][coord[1]] = self.coords[coord]

        return matrix
    def __str__(self):
        result = ""
        for y in range(0, self.height):
            for x in range(0, self.width):
                if sum(self.coords[(x,y)][:2])==0 or self.coords[(x,y)][3]==0:
                    result+="-"

                else:
                    result+="*"
            result+="\n"
        return result


a = picture()
print(a.get_matrix())
b = picture()
print(b.get_matrix())
print(Fore.RED+str(a))

`

0 个答案:

没有答案