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.

  • 1
    Why not a screenshot? Does this need to be automated? If so, what operating system are you using and what GUI? – terdon Jan 31 '22 at 13:05
  • @terdon Yes, I need to pipe it into the convertor routine, and do some processing on the image and present it elsewhere. I'm using Ubuntu which uses X but I simply thought it would be doable with imagemagick or something – Zeta.Investigator Jan 31 '22 at 13:09
  • Note that the example API I used provides an endpoint to download an image instead (by adding .png to the city name), but I want a generic solution. – Zeta.Investigator Jan 31 '22 at 13:13
  • 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 Jan 31 '22 at 13:15
  • @terdon doesn't xdotool interfere with current view? I prefer a solution which works non-invasively in background..using X or Wayland for this seems to be a hack... – Zeta.Investigator Jan 31 '22 at 13:23
  • 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 | ... – Zeta.Investigator Jan 31 '22 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 Jan 31 '22 at 13:34
  • @terdon Well, honestly it's not that I'm blocked by this issue, I just presented this example as the most generic case of this problem...but other CLI apps sometimes contain emojis, yes. Imagemagick has a :@- option but it does not work here (it doesn't render the colors): curl 'https://v2.wttr.in/Berlin' | convert label:@- out.png – Zeta.Investigator Jan 31 '22 at 13:39
  • 1
  • 1
    @Zeta.Investigator did you come up with a nice solution to this? I'm trying to do a very similar thing and have struggled. This one almost works for me: https://github.com/jiro4989/textimg but I've hit some unsupported characters. I'll try the python option from below next. – dtbaker Feb 26 '23 at 02:45
  • @dtbaker Unfortunately no. I just used the .png functionality shown in the answer below. That textimg is looks very nice though. – Zeta.Investigator Feb 26 '23 at 09:28

1 Answers1

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

Igor Chubin
  • 1,144