Gtk.jl icon indicating copy to clipboard operation
Gtk.jl copied to clipboard

making GtkImage from an array

Open Hugo-Trentesaux opened this issue 7 years ago • 4 comments

I'm new to both Julia and GTK and I can not find in the documentation how to create a GtkImage from an array.

To create an image from a file I do :

gtkimg = Gtk.GtkImage("filename");

I have an array representing an image and I would like to do

gtkimg = Gtk.GtkImage(array);

But it yields me a MethodError on GtkImageLeaf

Hugo-Trentesaux avatar Oct 21 '18 12:10 Hugo-Trentesaux

I have not used GtkImage so far. What I usually do is to draw to a canvas

http://juliagraphics.github.io/Gtk.jl/latest/manual/canvas.html

And starting from there you can use what Cairo.jl does offer.

tknopp avatar Oct 21 '18 13:10 tknopp

From the docs it look like you need to put the data in a GdkPixbuf . Looking at the sources we do have a constructor for GdkPixbuf that takes a matrix of colors (from the package Colors).

Otherwise GtkReactive has an example of an image viewer (with interactive zoom) that uses a canvas I think:

https://github.com/JuliaGizmos/GtkReactive.jl/blob/master/examples/imageviewer.jl

jonathanBieler avatar Oct 21 '18 18:10 jonathanBieler

Thanks for your responses !

@tknopp, It took me a little bit time, but I manage to do it with Cairo :

    Cairo.image(ctx, img32, 0, 0, w, h)

@jonathanBieler, I have seen the GdkPixbuf in the Gtk docs, but did not understand how to do it in Julia. The GtkReactive package seems to do the job well ! I will try to look deeper into it and I hope it can scale well. Otherwise, I will maybe write the Gtk part in C and make it dialog with Julia, but if I could avoid this without too many sacrifices, it would be fine !

Hugo-Trentesaux avatar Oct 22 '18 18:10 Hugo-Trentesaux

Here's a test that does this: https://github.com/JuliaGraphics/Gtk.jl/blob/7d90e89497eba88ebf6d80274a5828e8702ececc/test/gui.jl#L297-L305

You can write the pixbuf directly to update it, the only key point you need to remember is to force it to re-render afterwards. The easiest way to do that is just to set the image property of the pixbuf to the pixbuf again (basically, a no-op with a side-effect of marking the display region dirty).

vtjnash avatar Oct 22 '18 18:10 vtjnash