2015-10-13 49 views
0

我想通过套接字发送图像Pixbuf,但接收到的图像只有黑白和扭曲。 下面是我使用以下步骤:从GDK Pixbuf重新构建PNG图像

1)获取PIXBUF

2的像素阵列)的序列化的像素阵列

3)序列化的字符串转换为BytesIO

4 )寄过来的插座

MyShot = ScreenShot2() 
frame = MyShot.GetScreenShot() #this function returns the Pixbuf 
a = frame.get_pixels_array() 
Sframe = pickle.dumps(a, 1) 
b = BytesIO() 
b.write(Sframe) 
b.seek(0) 

在这之后我必须重建图像:

1)反序列化所接收到的字符串中原来的像素阵列

2)从像素阵列

3构建的pixbuf)保存图像

res = gtk.gdk.pixbuf_new_from_data(pickle.loads(b.getvalue()), frame.get_colorspace(), False, frame.get_bits_per_sample(), frame.get_width(), frame.get_height(), frame.get_rowstride()) #also tried this res = gtk.gdk.pixbuf_new_from_array(pickle.loads(b.read()),gtk.gdk.COLORSPACE_RGB,8) 
res.save("result.png","png") 

回答

0

如果你想发送Pixbuf通过套接字你必须发送全部数据,而不仅仅是像素。 BytesIO对象不是必需的,因为Numpy数组有一个tostring()方法。

发送PNG而不是发送原始数据并在接收端将其编码为PNG图像会更容易/更有意义。这里实际上需要一个BytesIO对象来避免临时文件。发送方:

screen = ScreenShot() 
image = screen.get_screenshot() 
png_file = BytesIO() 
image.save_to_callback(png_file.write) 
data = png_file.getvalue() 

然后发送data在插槽和接收方简单地将其保存:

with open('result.png', 'wb') as png_file: 
    png_file.write(data) 
+0

好主意,谢谢:) –