Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Command line popup with image

1,241 views
Skip to first unread message

Guillaume Dargaud

unread,
Dec 19, 2012, 5:41:05 AM12/19/12
to
Hello all,
I'd like to have a popup from a shell script that presents an image and asks
a simple [Yes/No] question. Something like "Is this the right image?"

I've looked at zenity and kdialog and I don't see any possibility to display
an image withing the dialog. Did I miss something or should I use another
popup tool ?

Thanks
--
Guillaume Dargaud
http://www.gdargaud.net/

Janis Papanagnou

unread,
Dec 19, 2012, 5:54:09 AM12/19/12
to
On 19.12.2012 11:41, Guillaume Dargaud wrote:
> Hello all,
> I'd like to have a popup from a shell script that presents an image and asks
> a simple [Yes/No] question. Something like "Is this the right image?"
>
> I've looked at zenity and kdialog and I don't see any possibility to display
> an image withing the dialog. Did I miss something or should I use another
> popup tool ?

Not sure what you're trying to do with the displayed image. Does this help...

VIEWER=gthumb # for example; use any viewer you like
IMAGE=someimage.png # whatever image
"$VIEWER" "$IMAGE" &
pid=$!
read yn?"is this the right image? "
printf "%s\n" "$yn"
kill "$pid"


Janis

>
> Thanks
>

Guillaume Dargaud

unread,
Dec 19, 2012, 8:23:00 AM12/19/12
to
Hello Janis,

yes it's pretty close to what I want, but the focus is shifted to the image,
forcing to alternate clic and keypress. I'd rather have a mouse only
solution (for instance with [Yes]/[No] buttons) or keyboard only.

If there is a way to tell the terminal window to stay in focus, that'd do.

Ben Bacarisse

unread,
Dec 19, 2012, 11:46:05 AM12/19/12
to
Guillaume Dargaud <use_the_co...@www.gdargaud.net> writes:

> Hello all,
> I'd like to have a popup from a shell script that presents an image and asks
> a simple [Yes/No] question. Something like "Is this the right image?"
>
> I've looked at zenity and kdialog and I don't see any possibility to display
> an image withing the dialog. Did I miss something or should I use another
> popup tool ?

I think you can, just about, do it with zenity. The key is to use a
text-info dialog with HTML as the text format:

zenity --text-info --html --filename=...

(you can add a "this is the right image" checkbox if you like).

However, there are two catches: (1) you must use a file name, and (2)
you don't seem to be able to use local file references in the image tag
(i.e. <img src="file:///home/ben/pic.jpg"> does not work).

If you want to avoid creating a temporary file you can get round (1)
using a trick like /dev/stdin provided your system supports it. To get
round (2) you can use an inline data URL. These look like this:

src="data:image/jpg;base64,...".

Here's an example:

(echo "<h1>Is this the right image?</h1><img src=\"data:"
mimetype -b "$1"
echo -n ";base64,"
base64 "$1"
echo "\">") | zenity --text-info --html --filename=/dev/stdin

--
Ben.

Guillaume Dargaud

unread,
Dec 21, 2012, 4:38:22 AM12/21/12
to
Hello Ben,

> I think you can, just about, do it with zenity. The key is to use a
> text-info dialog with HTML as the text format:
>
> zenity --text-info --html --filename=...

--html is not in the man page, so I would never have found that out.

> (you can add a "this is the right image" checkbox if you like).
>
> However, there are two catches: (1) you must use a file name, and (2)
> you don't seem to be able to use local file references in the image tag
> (i.e. <img src="file:///home/ben/pic.jpg"> does not work).
>
> If you want to avoid creating a temporary file you can get round (1)
> using a trick like /dev/stdin provided your system supports it. To get
> round (2) you can use an inline data URL. These look like this:

Nice trick with stdin, I would have tried something with --filename=<(...)

> src="data:image/jpg;base64,...".

I was vaguely aware of the possibility to inline binary code in html, but
I'd never used it.

> Here's an example:
>
> (echo "<h1>Is this the right image?</h1><img src=\"data:"
> mimetype -b "$1"
> echo -n ";base64,"
> base64 "$1"
> echo "\">") | zenity --text-info --html --filename=/dev/stdin

Thanks a lot, your example is spot on and it's great because with the html I
don't need to perform image processing beforehand to adjust the sizes.

I modified it to do what I wanted, a simple image compare popup:

# Call this with two file names
Size=300
Style=" STYLE=\"max-width:$Size; max-height:$Size\""
function Identical() {
(echo "<TABLE><TR><TD STYLE=\"width:100%; margin:0px auto;\"><IMG$Style
SRC=\"data:"
mimetype -b "$1"
echo -n ";base64,"
base64 "$1"
echo "\"></TD><TD><IMG$Style SRC=\"data:"
mimetype -b "$2"
echo -n ";base64,"
base64 "$2"
echo "\"></TD></TR></TABLE>"
) |
zenity --ok-label=Identical --cancel-label=Different --width=$(($Size*2+50))
--height=$(($Size+100)) --title="Are those two images identical?" --text-
info --html --filename=/dev/stdin;

Ben Bacarisse

unread,
Dec 21, 2012, 7:09:36 AM12/21/12
to
Guillaume Dargaud <use_the_co...@www.gdargaud.net> writes:

>> I think you can, just about, do it with zenity. The key is to use a
>> text-info dialog with HTML as the text format:
>>
>> zenity --text-info --html --filename=...
>
> --html is not in the man page, so I would never have found that out.

So I see! I found out about it by reading the online manual at the
Gnome website; and I only went there just because I find the format of
the zeinity man page a little hard to follow. A happy accident as it
turns out. If I get time, I might try to find the man page and submit a
patch.

>> (you can add a "this is the right image" checkbox if you like).
>>
>> However, there are two catches: (1) you must use a file name, and (2)
>> you don't seem to be able to use local file references in the image tag
>> (i.e. <img src="file:///home/ben/pic.jpg"> does not work).
>>
>> If you want to avoid creating a temporary file you can get round (1)
>> using a trick like /dev/stdin provided your system supports it. To get
>> round (2) you can use an inline data URL. These look like this:
>
> Nice trick with stdin, I would have tried something with
> --filename=<(...)

It just depends on whether you want to rely on a shell feature or a
system feature.

Over the years I've used zeninty quite a bit, and I find the design a
little annoying at times. The fact that a text-info dialogue won't read
standard input seems like an odd decision.

<snip>
--
Ben.
0 new messages