Skip to main content
 首页 » 编程设计

python之Gtk+3 Gdk Pixbuf.new_from_data 给出 "Segmentation fault (core dumped)"错误 139

2025年05月04日11虾米哥

在我的应用程序中,我有一个 GtkImage,它必须显示来自所选文件的已处理图像。因此,在处理程序部分我有:

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) 

至此,一切正常。如果我们这样做:

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

预期的图像被粘贴到 GtkImage 小部件上。 但这是丑陋的方式,不是吗?

所以我正在尝试:

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]) 

但结果是“错误 139,段错误(核心已转储)”

我错过了什么?

请您参考如下方法:

这似乎与这个 gdk 错误有关:https://bugzilla.gnome.org/show_bug.cgi?id=721497

基本上它是 gdk 的 python 包装器中的一个免费后使用错误,它可能会导致图像失真和/或段错误,就像它为您所做的那样。 请参阅:https://stackoverflow.com/a/24070152/3528174

您可以在这个问题中找到此类图像失真的示例:How to correctly covert 3d array into continguous rgb bytes