简体   繁体   English

相当于PIL.Image tobytes()的python gtk

[英]python gtk equivalent of PIL.Image tobytes()

what is gtk equivalent of PIL.Image tobytes()? gtk等于PIL.Image tobytes()?

i = PIL.Image.open('image.png')
data = i.tobytes() 

I am trying to get screen with GTK and transform it to the same bytes as in previous PIL: 我正在尝试使用GTK获取屏幕并将其转换为与以前的PIL相同的字节:

import gtk, PIL.Image

img_width = gtk.gdk.screen_width()
img_height = gtk.gdk.screen_height()
data = gtk.gdk.Pixbuf(
  gtk.gdk.COLORSPACE_RGB,
  False,
  8,
  img_width,
  img_height
)  
data.get_from_drawable(
  gtk.gdk.get_default_root_window(),
  gtk.gdk.colormap_get_system(),
  0, 0, 0, 0,
  img_width,
  img_height
)
data = data.tobytes() #<-- here trying to get bytes

You're looking for pixbuf.get_pixels() . 您正在寻找pixbuf.get_pixels()

Note that it's possible for the returned data to contain padding: 请注意,返回的数据可能包含填充:

Rows in the image are stored top to bottom, and in each row pixels are stored from left to right. 图像中的行从上到下存储,在每一行中,像素从左到右存储。 There may be padding at the end of a row. 在一行的末尾可能会有填充。 The "rowstride" value of a pixbuf, as returned by gdk_pixbuf_get_rowstride(), indicates the number of bytes between rows. 由gdk_pixbuf_get_rowstride()返回的pixbuf的“ rowstride”值指示行之间的字节数。

(from here .) (从这里开始 。)

You can use string/bytes operations to remove this padding, or alternatively use pixbuf.save_to_callback to save the image in a raw RGB format with no padding. 您可以使用字符串/字节操作删除此填充,或者使用pixbuf.save_to_callback将图像保存为原始RGB格式,而无需填充。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM