16

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.

10
  • 3
    What kind of terminal do you have? A DEC GT40?
    – Anthon
    Commented Jul 25, 2014 at 6:52
  • @Anthon Gnome-Terminal (that comes with Ubuntu) Commented Jul 25, 2014 at 6:56
  • 1
    @Anthon A DEC GT40 would be useful, but it's too expensive... :-) Commented Jul 25, 2014 at 6:57
  • 1
    @terdon I voted to reopen because this isn't intrinsically a programming question (I did remove the c and 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. Commented Jul 25, 2014 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
    Commented Jul 25, 2014 at 18:16

6 Answers 6

17

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())
1
  • 2
    Whilst this may theoretically answer the question, it would be preferable to include the essential parts of the answer here, and provide the link for reference.
    – slm
    Commented Sep 30, 2015 at 3:25
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.

3
  • 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) Commented Jul 25, 2014 at 8:41
  • 3
    Cool! Some enthusiasts draw 3d teapot in xterm =) dim13.org/2009/02/teapot
    – gena2x
    Commented Jul 25, 2014 at 11:02
  • If the terminal can only "draw" chars, then maybe the pixel should be made a char so it can be drawn...
    – user44370
    Commented Jul 25, 2014 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
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
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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .