11

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 Jul 25 '14 at 6:52
  • @Anthon Gnome-Terminal (that comes with Ubuntu) – Ionică Bizău Jul 25 '14 at 6:56
  • 1
    @Anthon A DEC GT40 would be useful, but it's too expensive... :-) – Ionică Bizău Jul 25 '14 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. – 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
11

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
  • 7
    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 8:41
  • 2
    Cool! Some enthusiasts draw 3d teapot in xterm =) 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... – user44370 Jul 25 '14 at 21:23
12

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
  • 1
    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 Sep 30 '15 at 3:25
3

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, ...).

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.