从 GDK Pixbuf 重建一个 png 图像
Re-Construct a png image from a GDK Pixbuf
我想通过套接字发送图像 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")
如果您想通过套接字发送 Pixbuf
,您必须发送 所有 数据,而不仅仅是像素。 BytesIO
对象不是必需的,因为 Numpy 数组有一个 tostring()
方法。
发送 PNG 而不是发送原始数据并在接收端将其编码为 PNG 图像会更有意义 easier/make。这里实际上需要一个 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)
我想通过套接字发送图像 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")
如果您想通过套接字发送 Pixbuf
,您必须发送 所有 数据,而不仅仅是像素。 BytesIO
对象不是必需的,因为 Numpy 数组有一个 tostring()
方法。
发送 PNG 而不是发送原始数据并在接收端将其编码为 PNG 图像会更有意义 easier/make。这里实际上需要一个 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)