Redirect terminal output to image file

7,178

For commands with little output and short lines you can do that easily with a combination of a2ps, ghostscript and imagemagick:

Here the output of the command ls is used as an example.

ls | 
a2ps -=book -B -q --medium=A4dj --borders=no -o out1.ps &&
gs \
  -sDEVICE=png256           \
  -dNOPAUSE -dBATCH -dSAFER \
  -dTextAlphaBits=4 -q      \
  -r300x300                 \
  -sOutputFile=out2.png out1.ps
convert -trim out2.png result.png

a2ps creates a vector image of the text. ghostscript rasterises it into a PNG graphic (don't use JPEG, it's the wrong graphic format for this, it's only useful for photorealistic images). Finally imagemagick is used to remove the surrounding white space. Read the man pages and tweak the parameters as necessary.

If the programs are not installed, you can compile and install them with --prefix=/home/me/.local as a user without admin rights.

EDIT: As mentioned in a comment a solution without the dependency of a2ps and ghostscript is the following one.

convert label:"$(ls)" result.png
Share:
7,178

Related videos on Youtube

Fernando Martinez
Author by

Fernando Martinez

Updated on September 18, 2022

Comments

  • Fernando Martinez
    Fernando Martinez over 1 year

    I need to programmatically run some unix commands and get the output in a image file, the format could be png or jpeg (jpg).

    The commands are run in an AIX (IBM *nix) machine. I don't have permission to install new packages, however I think there a way to do this using a pipeline and redirections with the default packages from the Operating System.

    Unfortunately I couldn't find a method to do this.

    • donothingsuccessfully
      donothingsuccessfully almost 12 years
      Take screenshots and edit them together on a more fully featured computer.
  • Fernando Martinez
    Fernando Martinez almost 12 years
    Thank you for your answer, but how as I indicated, I don't have permission to install new packages
  • donothingsuccessfully
    donothingsuccessfully almost 12 years
    If you have imagemagick you could cut out the a2ps and just use convert label:"$( ls )" ls_output.png. I can't speak as to the aesthetics though.
  • Marco
    Marco almost 12 years
    @FernandoMartinez You don't need root privileges to install software, except if the home partition is mounted with noexec, but I've never seen a system in production where the users had no execute rights in their home directory. You should double-check that.
  • Fernando Martinez
    Fernando Martinez almost 12 years
    I didn't know this trick, I run "$(ls)" result.png, is it correct?, Do you have some examples?. Thank you.
  • Marco
    Marco almost 12 years
    The example is printed on the gray area in my answer. You need the program convert from the imagemagick suite for that.
  • Fernando Martinez
    Fernando Martinez almost 12 years
    But as I said I can install extra package, so I can use imagemagick
  • David Bern
    David Bern over 3 years
    Perfect results. smooth solution.