jaq icon indicating copy to clipboard operation
jaq copied to clipboard

Display image values using Sixel terminal protocol

Open gwpl opened this issue 9 months ago • 10 comments

Requesting here as https://github.com/jqlang/jq/discussions/3291 maybe also great addition to this project:

Sixel allows to display images in terminals ( e.g. https://github.com/saitoha/libsixel ; https://en.wikipedia.org/wiki/Sixel ; https://github.com/atanunq/viu (Rust) ), let's make --sixel flag that will :

  • under each value (in pretty print mode) that contains image (e.g. base64 encoded) to display "key-name:" and after that display image using sixel
  • that would require detecting if given value is base64 and later if is some supported image file (e.g. png, jpg, webp, etc) (e.g. --img auto flag)
  • alternatively pointing manually at keys to try to decode as image ( --img .data.key.img )

Rationale:

  • more and more APIs return image encoded as value, or AI/ML datasets, contain images in json files as values.

gwpl avatar Mar 08 '25 23:03 gwpl

If you decide to add support you can always reply to https://x.com/GWierzowiecki/status/1898510522221461577 :) Thank you!

gwpl avatar Mar 08 '25 23:03 gwpl

Btw. I gave a try in

  • https://github.com/VariousForks/jaq/tree/issue-267-sixel-images branch ( jaq --img auto < with_images.json )

with this test payload json with base64 png image as one of values:

  • Example JSON with PNG base64 value: https://gist.github.com/gwpl/f770639a0a21025ec5c1c775a200ddc8

Currently it "almost works" , in my terminal it just jumps to 0x0 cortner then displaying image, instead of behaving like viu ( https://github.com/atanunq/viu - that uses viuer btw.) or lsix, which display image in next line, and continue with printing text after image...

gwpl avatar Mar 09 '25 00:03 gwpl

Hi @gwpl, thanks a lot for the idea and your work on the sixel branch! Funnily, just today, before seeing your post, I thought about precisely the same functionality (showing image data directly from jaq), so I'm definitely interested. I think that to be flexible, I would not add a command-line flag, but a filter that interprets an image as image, e.g. .img |= (@base64d | @pngd). I can adapt your work to this, once you figure out how to make the image appear at the right position.

01mf02 avatar Mar 10 '25 18:03 01mf02

Lovely! Please, I don't know when I will have spare moment to sit and debug this, if I will I will do, otherwise as you see I made ticket on repo of library asking for help, so hopefully it will work out... ... otherwise, please try viu command line tool yourself that is CLI wrapper around library I used, and maybe that will give you a feel how to make it work. Also I don't mind if anyone will make new branch and drop mine, I just would love to see that feature , and hopefully my work will be good starting point or inspiration to others. Otherwise, if spare moment comes, will try to push debugging deeper. (or if someone finds issue please write in this issue I am happy to apply changes, advice to my branch and update it)

gwpl avatar Mar 10 '25 19:03 gwpl

@gwpl, do not worry and take your time, if you want to continue your effort on this feature. I'm not in a hurry. :)

01mf02 avatar Mar 11 '25 15:03 01mf02

I've found fix!

Works! Pull Reuqest: https://github.com/01mf02/jaq/pull/269

Image

gwpl avatar Mar 11 '25 19:03 gwpl

Wow, thanks for your work, this looks already quite good! I have left a few comments in your PR to help you improve your code.

01mf02 avatar Mar 14 '25 08:03 01mf02

More updates on https://github.com/01mf02/jaq/pull/269 !

Image 2025-03-14--jaq-issue-267-with-identation.json

(updating gist with used jsons with images: https://gist.github.com/gwpl/f770639a0a21025ec5c1c775a200ddc8 )

gwpl avatar Mar 14 '25 20:03 gwpl

Hello, can I help with review? IIUC Convention is to respond to reviewer comments, and reviewer to close discussions, however if you wish otherwise, I am glad to help and follow.

gwpl avatar Mar 26 '25 20:03 gwpl

@gwpl, see my comment in #269.

01mf02 avatar Mar 27 '25 10:03 01mf02