在我的应用程序中,我有一个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,分段错误(核心转储)”
我错过了什么?
答案 0 :(得分:0)
这似乎与此gdk错误有关:https://bugzilla.gnome.org/show_bug.cgi?id=721497
基本上它是在gdk的python包装器中的free bug之后使用,它可能导致图像失真和/或段错误。 请参阅:https://stackoverflow.com/a/24070152/3528174
您可以在此问题中找到此类图像扭曲的示例:How to correctly covert 3d array into continguous rgb bytes