3

Suppose the CLI application my_bin produces an output in terminal which consists of colored text, Unicode symbols and emojis.

How can I convert the output to a svg or an image on Ubuntu?

For example, curl "v2.wttr.in/Berlin" output is like this in the terminal:

output of the command

How can I convert it to an image file (e.g. vectored image file, png, svg, etc)?

Update:

  • I need a solution that doesn't actually need rendering the output of the command in a terminal window. So it should work in background without having to utilize X or Wayland to capture its view.

  • Tried curl "v2.wttr.in/Berlin" | pbmtext | pnmtopng > out.png
    This does not render colors or emojis

  • Tried pango-view --font='mono' -qo out.png <(curl v2.wttr.in/Berlin)
    It renders emojis but does not render terminal escape codes for colors.

11
  • 1
    Why not a screenshot? Does this need to be automated? If so, what operating system are you using and what GUI?
    – terdon
    Commented Jan 31, 2022 at 13:05
  • 1
    It should be possible with imagemagic or even with tools like lpr or enscript. I'm having trouble getting it to work though so a possible workaround would be to use xdotool or similar to open the file and trigger a screenshot.
    – terdon
    Commented Jan 31, 2022 at 13:15
  • 1
    @Quasímodo Thanks for your suggestion but the import tool from imagemagick needs a rendered window. I want a solution that doesn't need the actual output to be rendered anywhere...like my_app | convert_to_image | ... Commented Jan 31, 2022 at 13:25
  • 1
    @Zeta.Investigator oh it is absolutely a hack, no argument there. But it might be a decent hack, that's all. But yes, you're right, it will interfere with your normal view. Hmm... The thing is that this particular example is unicode so that makes everything more complicated. Is this essential? Since you said that this API gives an image, will others that do not give images also produce unicode?
    – terdon
    Commented Jan 31, 2022 at 13:34
  • 1
    Maybe some of these can help: How to transform a text file into a picture
    – terdon
    Commented Jan 31, 2022 at 13:45

1 Answer 1

1

wttr.in author is here.

First of all, if it is wttr.in output that you want to convert to a picture, and nothing more, the task is trivial, because wttr.in itself has a PNG frontend. To use it, just append .png to the end of the query:

wget v2.wttr.in/Berlin.png

If you want to convert any terminal output to PNG, the task is more complex, and its solution depends on several factors:

  1. Do you need colors support?
  2. If yes, how may colors must be supported? (there are several various ANSI color encoding methods for that)
  3. Do you need emojis support?
  4. What scripts do you want to support? Is it only Latin, or is it more?
  5. Do you need support for embedded terminal graphics?

In trivial case, where the answer for all questions here is no, it is pretty easy, and there are a lot of programs doing there.

In the opposite case, where you want to support as much as possible, the task may become pretty complicated. To solve this task for wttr.in, I wrote a custom rendering engine, which supports 1,2,3 and 4, except some complex eastern scripts like Devanagari, and Arabic. It is implemented in Python, and it uses PIL for graphics manipulation and pyte, for the virtual terminal rendering. You can find it in the wttr.in source code:

https://github.com/chubin/wttr.in/blob/master/lib/fmt/png.py

2
  • Thanks! (and your project is awesome!) Yeah I knew about the png format...My question tries to be as generic as possible. For simple cases, I provided some solutions in the question itself. Commented Feb 6, 2022 at 12:33
  • I've added a link to the png rendering code. Commented Feb 6, 2022 at 13:04

You must log in to answer this question.

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