15

Until now I used characters to draw images, shapes etc in a terminal.

Is it possible to draw a single pixel?

Let's say:

foo 1 1 red

This will draw a red pixel at the coordinate (1, 1).

Is there an existing application that will do this job?

Currently running Ubuntu 14.04.

  • 3
    What kind of terminal do you have? A DEC GT40? – Anthon Jul 25 '14 at 06:52
  • @Anthon Gnome-Terminal (that comes with Ubuntu) – Ionică Bizău Jul 25 '14 at 06:56
  • 1
    @Anthon A DEC GT40 would be useful, but it's too expensive... :-) – Ionică Bizău Jul 25 '14 at 06:57
  • This question appears to be off-topic because it is essentially a programming question. It is also off topic on SO though since there is no code shown. I am therefore closing with no migration. – terdon Jul 25 '14 at 11:30
  • 1
    @terdon I voted to reopen because this isn't intrinsically a programming question (I did remove the [tag:c] and [tag:c++] tag, because if you're going to write C or C++ code, that is off-topic here). “There's no such feature, you'd need to program it” would be a proper answer here — except that it would be wrong: a good answer would mention Tektronix and drawing over the terminal window like w3m does. – Gilles 'SO- stop being evil' Jul 25 '14 at 17:52
  • 1
    @Gilles fair enough, I've reopened. I closed it because it was in the process of being migrated to SO and it was not welcome there. – terdon Jul 25 '14 at 18:16
  • @terdon As a programming task, it should have been welcome there. As an SO user, I very much hope that that meta post is the start of a trend to get SO out of the debugging-posts-only rut that it has fallen into. – Gilles 'SO- stop being evil' Jul 25 '14 at 18:30
  • @Gilles I asked a mod who said it wasn't. – terdon Jul 25 '14 at 18:31
  • @Gilles - not just TEK, though Graeme covers it and Sixel support pretty well here. My own answer to that linked question was about Terminology, which supports extended escapes that enable you to mask out a map region of screen space then ask it to fill the region with an image, video, whatever that you specify. Different than w3m which calls X to draw to fb, I think, in this case the terminal does the drawing itself. xterm implemented some similar features in change #305 June this year. – mikeserv Jul 26 '14 at 00:02

6 Answers6

16

Have a look at the Drawille library. It uses the UTF braille characters to draw pixels.

Something like the following would set a single pixel:

from drawille import Canvas

c = Canvas()

c.set(10, 10)

print(c.frame())
mipmip
  • 281
13

Terminals are character-cell displays and don't support drawing pixel graphics. Not even when running in X11; although it's certainly possible to draw individual pixels when talking directly to an X server, if your program is talking to a terminal it can only ask the terminal to display characters.

To display graphics instead of text, you'll need to write a program that interacts directly with the X server. This is typically done through a UI toolkit library such as GTK, Qt, or wxWidgets.

Wyzard
  • 2,404
  • 8
    There are all sorts of terminals, some with graphical capabilities (like the Tektronix mode of xterm, or the embedding of images of terminology). Note that terminal also refers to X11 terminals (the successors of the things like the Tektronix ones) – Stéphane Chazelas Jul 25 '14 at 08:41
  • 3
    Cool! Some enthusiasts draw 3d teapot in xterm =) http://www.dim13.org/2009/02/teapot – gena2x Jul 25 '14 at 11:02
  • If the terminal can only "draw" chars, then maybe the pixel should be made a char so it can be drawn... –  Jul 25 '14 at 21:23
5

You won't be able to draw single pixel colors in the terminal unless you can do what Wyzard's mentions, program it yourself, or find a tool already made for the job (this could be terminal-specific). However it is possible to use individual character coordinates in your terminal to draw 2D images using ASCII and UTF-8 characters. The tool for this is called tput. This tool works by manipulating the cursor position according to the coordinates of your current terminal. Here is a sample list of tput functionalities:

# 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
Yokai
  • 223
2

In addition of the major widget toolkits (GTK, Qt, WxWidgets) mentioned in Wyzard's answer you could also consider more media oriented libraries like SFML or libSDL. If you just want some graphical output (with almost no interaction) you might even consider CairoGraphics.

In some cases you might use, thru a pipe (e.g. popen) or on the command line, command utilities like GNUplot or GraphViz.

You could also consider using the graphical abilities of your browser, e.g. by coding some dedicated web application (using some HTTP server library like Wt or libonion, with web technologies like AJAX, SVG, HTML5, canvas, websockets, etc...)

But most terminal emulators have no or too little graphical abilities (so no genuine pixel graphics). So you practically need your program to interact with the display server (X11, Wayland, ...).

1

Yes. there is no existing low level api support to meddle with pixels/likewise in a terminal environment.

However, this project: wttr.in deserves particular attention: Try these commands and see for yourself!

curl "wttr.in?format=v2"
curl wttr.in


Bottomline

  • The output from the commands run in a terminal app return a stream of characters (with a defined character-set).
  • The characters -> pixels are displayed on the terminal's rectangle by the font-configuration, ANSI-sequences & other app specific settings.
  • With clever exploration of the character-set which includes unicode characters & emoji sequences; one has some artistic legroom to wiggle.
# Check the raw output as well.
curl "wttr.in?format=v2" > weather-raw.txt
dsculptor
  • 126
0

Solution depends on what terminal you'll do it. If pure shell then you can use linux framebuffer to draw(example here). Use sudo in that way because framebuffer device is not accessable for regular user only for root. If you trie to draw in GUI terminal you may look at xv source code. It draws images in terminal using X11 functions. It may not work with some terminals emulators but almost all supports it. Also you can generates images and draw it with xv. In such way you do not need to understand how it works.