自制python 终端画布模块

python在终端绘制一个画布:consoleCanvas

效果如图 :

 

已发布到pypi

https://pypi.org/project/ConsoleCanvas/

 

Project description

ConsoleCanvas用法:

0.安装

pip install ConsoleCanvas -i https://pypi.python.org/simple

 

0.1.终端显示cv2的img:

import consoleCanvas,cv2
img=cv2.imread("C:\\Users\\Administrator\\Desktop\\1.png")
consoleCanvas.cvShow(img)

1.先初始化对象:

import consoleCanvas
A=consoleCanvas.consoleCanvas()

 

2.生成画布:

A.ProduceCanvas(80,80)#创建画布

 

3.绘制像素点:

A.reviseCanvas([x,y],1)
#[x,y]是坐标,1是代表绘制黑色点,0是绘制白色点

 

4.显示画布:

A.show() #显示画布

 

5.清空画布:

A.ProduceCanvas(80,80)#也就是重新创建画布

 

6.例子

#绘制圆形
import consoleCanvas
a=10
b=10
r=10
A=consoleCanvas.consoleCanvas()#初始化
A.ProduceCanvas(21,21)#创建画布
A.reviseCanvas([0,0],1)
for x in range(a-r,a+r):
    y=int((((r**2)-(x-a)**2)**(1/2))+b)
    A.reviseCanvas([x,y],1)#绘制画布像素
for x in range(a+r,a-r,-1):
    y=int(-1*(((r**2)-(x-a)**2)**(1/2))+b)
    A.reviseCanvas([x,y],1)#绘制画布像素
A.show() #显示画布

 

输出:

⠁⠀⠔⠀⠀⠁⠀⠂⠄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠠⠃⠀⠀⠀⠀⠀⠀⠈⠆⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠇⠀⠀⠀⠀⠀⠀⠀⠀⠸⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠇⠀⠀⠀⠀⠀⠀⠀⠀⠨⠂⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠇⠀⠀⠀⠀⠀⠀⠀⠀⠸⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠈⠆⠀⠀⠀⠀⠀⠀⠠⠃⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠑⠀⠀⠄⠀⠂⠁⠀

 

用法:

1.先初始化对象:

A=consoleCanvas()

2.生成画布:

A.ProduceCanvas(80,80)#创建画布

3.绘制像素点:

A.reviseCanvas([x,y],1)
#[x,y]是坐标,1是代表绘制黑色点,0是绘制白色点

4.显示画布:

A.show() #显示画布

5.清空画布:

A.ProduceCanvas(80,80)#也就是重新创建画布

 

 

Python示例代码:

class consoleCanvas:
    def __init__(self):
        #这些字符是盲文字符
        self.li=["", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
            "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
            "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
            "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",

            "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
            "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
            "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
            "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",

            "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
            "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
            "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
            "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",

            "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
            "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
            "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
            "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""]
    def ProduceCanvas(self,Width, height):
        self.map_c=[[0 for i in range(80)]  for i in range(80)]
        return self.map_c

    def reviseCanvas(self,position, data):
        self.map_c[position[0]][position[1]]=data
        return self

    def getnum(self,data):
        #匹配字符索引
        q=[
        [1,8],
        [2,16],
        [4,32],
        [64,128],]
        num=0
        for w in range(len(data)):
            for h in range(len(data[w])):
                if (data[w][h]==1):
                    num+=q[w][h]
        return num


    def show(self,map_c=""):
        if map_c=="":
            map_c=self.map_c
        list_data=[]
        for wi in range((len(map_c)%3)+(len(map_c)%3)):#补齐行数不足宽度
            map_c.append([0 for i in range(len(map_c[0]))])

        for hi in map_c:##补齐列数不足宽度
            for n in range((len(map_c)%3)+(len(map_c)%3)):
                hi.append(0)

        for w in range(0,len(map_c),3): #切片地图,变换地图
            lis=[]
            for i in range(0,len(map_c[w]),2):
                lis.append([map_c[w][i:i+2],map_c[w+1][i:i+2],map_c[w+2][i:i+2]])
                
            list_data.append(lis)

        for  i in list_data: #显示地图
            for x in i:
                num=self.getnum(x)
                print(self.li[num],end="")
            print()

if __name__ == "__main__":
    
    
    A=consoleCanvas()#初始化
    A.ProduceCanvas(80,80)#创建画布

    a=50
    b=50
    r=20
    
    for x in range(a-r,a+r):
        y=int((((r**2)-(x-a)**2)**(1/2))+b)
        A.reviseCanvas([x,y],1)#绘制画布像素
    for x in range(a+r,a-r,-1):
        y=int(-1*(((r**2)-(x-a)**2)**(1/2))+b)
        A.reviseCanvas([x,y],1)#绘制画布像素

    A.show() #显示画布

 

posted @ 2020-08-28 17:44  sunny开始学坏  阅读(971)  评论(0编辑  收藏  举报