3

In my application I have a GtkImage that must show the processed image from a choosed file. So, in the handlers section I have:

import numpy as np
from PIL import Image , ImageDraw
from gi.repository import Gtk,  GdkPixbuf
. . .
. . .
def on_fitchooserdialog_response(self, menuitem, data=None):
    if data == 1:  
        self.fitlist = self.fitchooser.get_filenames()
        # get data from 1st file:
        _, self.data = Getdata(self.fitlist[0])
        # convert from Fits to 2D array:
        pngarray = Fit2png(self.data)
        # rescale:
        size = tuple(x/2 for x in pngarray.shape)
        im = Image.fromarray(pngarray)
        im.thumbnail((size[1],size[0]), Image.BICUBIC)

Up to here, all is OK. If we do:

       im.save("../tmp/tmp.png")
       pixbuf = GdkPixbuf.Pixbuf.new_from_file('../tmp/tmp.png')
       self.imagen.set_property("pixbuf", pixbuf)

the expected image is pasted on the GtkImage widget. But that is the ugly way, isnt it?

So Im trying:

im = im.convert("RGB")
arr = np.array(im).flatten()
pixbuf = GdkPixbuf.Pixbuf.new_from_data(arr,
      GdkPixbuf.Colorspace.RGB, False, 8, size[1], size[0], 3*size[1])

But the result is "Error 139, Segmentation fault (core dumped)"

What am I missing?

Rodolfo Leibner
  • 171
  • 1
  • 7
  • I am having same issue but in a different context : http://stackoverflow.com/questions/30069275/opencv-pygi-how-to-display-an-image-read-by-opencv. It looks like also this is not GdkPixbuf.Pixbuf.new_from_data that triggers the core dump, but rather when the pixbuf is used to fuel the image. – Pierre G. May 07 '15 at 19:07
  • to convert `numpy` data to pixbuffers you can use https://stackoverflow.com/questions/39936737/how-to-turn-gdk-pixbuf-object-into-numpy-array/41714464#41714464 – am70 Jun 11 '20 at 11:42
  • to convert `numpy` data to pixbuffers you can use https://stackoverflow.com/questions/39936737/how-to-turn-gdk-pixbuf-object-into-numpy-array/41714464#41714464 – am70 Jun 11 '20 at 11:43

1 Answers1

3

This seems to be related to this gdk bug: https://bugzilla.gnome.org/show_bug.cgi?id=721497

Basically it is a use after free bug in the python wrapper of gdk which can result in image distortions and/or segfaults as it did for you. See: https://stackoverflow.com/a/24070152/3528174

You can find an example of such image distortions in this question: How to correctly covert 3d array into continguous rgb bytes

Community
  • 1
  • 1
Thilo
  • 234
  • 1
  • 8
  • There is a discussion about this GTK problem with a few tips here as well: https://gitlab.gnome.org/GNOME/pygobject/issues/225 – Zoltan Jul 28 '19 at 14:09