【问题标题】:Re-Construct a png image from a GDK Pixbuf从 GDK Pixbuf 重建 png 图像
【发布时间】:2015-10-13 13:20:51
【问题描述】:

我想通过套接字发送图像 Pixbuf,但接收到的图像只有黑白且失真。 以下是我正在使用的步骤:

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) 从像素数组构建 Pixbuf

3) 保存图片

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

【问题讨论】:

    标签: python python-2.7 pickle gdkpixbuf bytesio


    【解决方案1】:

    如果您想通过套接字发送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)
    

    【讨论】:

      猜你喜欢
      • 2022-07-18
      • 1970-01-01
      • 2013-11-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-04
      相关资源
      最近更新 更多