2017-06-18 28 views
0

什麼是gtk相當於PIL.Image tobytes()?python gtk相當於PIL.Image tobytes()

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

我試圖讓屏幕與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 
+0

您是否知道您使用的是過時版本的gtk?當前版本是Gtk3,您可以通過'從gi.repository import Gtk'導入。 –

回答

0

您正在尋找pixbuf.get_pixels()

注意,有可能爲返回的數據包含填充:

行的圖像存儲自上而下,並在每一行像素 存儲由左到右。在一行的末尾可能會有填充。 由 gdk_pixbuf_get_rowstride()返回的pixbuf的「rowstride」值指示 行之間的字節數。

(來自here。)

可以使用串/字節操作刪除此填充,或可替代地使用pixbuf.save_to_callback將圖像保存在沒有填充一個原始RGB格式。

+0

優秀!謝謝 – peter