到目前为止,我使用字符在终端中绘制图像、形状等。
是否可以绘制单个像素?
让我们说:
foo 1 1 red
Run Code Online (Sandbox Code Playgroud)
这将在坐标处绘制一个红色像素(1, 1)
。
是否有现有的应用程序可以完成这项工作?
当前运行 Ubuntu 14.04。
小智 14
看看Drawille 库。它使用 UTF 盲文字符来绘制像素。
类似以下内容将设置单个像素:
from drawille import Canvas
c = Canvas()
c.set(10, 10)
print(c.frame())
Run Code Online (Sandbox Code Playgroud)
Wyz*_*ard 11
终端是字符单元显示,不支持绘制像素图形。甚至在 X11 中运行时也不行;虽然在直接与 X 服务器对话时当然可以绘制单个像素,但如果您的程序正在与终端对话,它只能要求终端显示字符。
要显示图形而不是文本,您需要编写一个直接与 X 服务器交互的程序。这通常是通过 UI 工具包库完成的,例如GTK、Qt或wxWidgets。
您将无法在终端中绘制单个像素颜色,除非您可以执行Wyzard提到的操作、自己编程或找到已为该工作制作的工具(这可能是特定于终端的)。但是,可以在终端中使用单个字符坐标来使用 ASCII 和 UTF-8 字符绘制 2D 图像。用于此目的的工具称为tput
. 该工具的工作原理是根据当前终端的坐标操纵光标位置。以下是功能示例列表tput
:
# tput Cursor Movement Capabilities:
tput cup Y X
# Move cursor to screen location X,Y (top left is 0,0)
tput sc
# Save the cursor position
tput rc
# Restore the cursor position
tput lines
# Output the number of lines of the terminal
tput cols
# Output the number of columns of the terminal
tput cub N
# Move N characters left
tput cuf N
# Move N characters right
tput cuu N
# up N lines
tput cud N
# down N lines
Run Code Online (Sandbox Code Playgroud)