2012-04-26 42 views
5

Làm cách nào để chuyển đổi hình ảnh PIL trong pixbuf ?. Tôi cố gắng để thay đổi rất nhiều ví dụ nhưng không có giải phápChuyển đổi PIL GdkPixbuf

import array 
from gi.repository import GdkPixbuf 

def image2pixbuf(self,im): 
    arr = array.array('B', im.tostring()) 
    height, width = im.size 
    return GdkPixbuf.Pixbuf.new_from_data(arr, GdkPixbuf.Colorspace.RGB, 
             True, 8, width, height, width * 4) 

Lỗi

TypeError: new_from_data() takes Exactly 9 arguments (7 given) 

tôi sử dụng Pygobject https://live.gnome.org/PyGObject

Trả lời

1

Tôi đã gặp sự cố tương tự với Gtk3. GdkPixbuf.PixbufDestroyNotify - NotImplementedError - python gtk3

Dường như có lỗi. Tôi đã báo cáo nó. https://bugzilla.gnome.org/show_bug.cgi?id=674691

Gtk3 có lỗi này. Nhưng bạn vẫn có thể làm điều đó trong Gtk2.

+0

Nhận xét này từ báo cáo lỗi có thể có ích: "để bạn có thể vẫn sử dụng' new_from_file() ', hoặc chỉ 'mới()' và thiết lập dữ liệu sau " –

+1

Vâng, tôi đã thấy điều đó. Nhưng tôi đặc biệt muốn gdkPixbuf new_from_data() – Froyo

1

Đây là cách tôi đã làm nó trong PyGTK, có lẽ đây vẫn hoạt động (tôi Copy đoạn mã sau pygi-convert'ed nó):

import Image 
import StringIO 
from gi.repository import GdkPixbuf 

def thumbnail_image(self, image): 
    """Creates a thumbnail GdkPixbuf of given image""" 

    # Create thumbnail 
    img = Image.open(image) 
    img.thumbnail((200, 300), Image.ANTIALIAS) 

    # Convert to GdkPixbuf 
    if img.mode != 'RGB':   # Fix IOError: cannot write mode P as PPM 
     img = img.convert('RGB') 
    buff = StringIO.StringIO() 
    img.save(buff, 'ppm') 
    contents = buff.getvalue() 
    buff.close() 
    loader = GdkPixbuf.PixbufLoader('pnm') 
    loader.write(contents, len(contents)) 
    pixbuf = loader.get_pixbuf() 
    loader.close() 

    return pixbuf 

Kind coi


EDIT : Ok, điều này có vẻ là làm việc ... Tôi chỉ rất ghét PyGObject nghèo C ported API (cho đến bây giờ ...).

Mã (test.py):

import Image 
import StringIO 
from gi.repository import Gtk, GdkPixbuf 
from os.path import abspath, dirname, join 

WHERE_AM_I = abspath(dirname(__file__)) 

class MyApp(object): 

    def __init__(self): 
     # Build GUI 
     self.builder = Gtk.Builder() 
     self.glade_file = join(WHERE_AM_I, 'test.glade') 
     self.builder.add_from_file(self.glade_file) 

     # Get objects 
     go = self.builder.get_object 
     self.window = go('window') 
     self.image = go('image') 

     # Load image 
     path = join(WHERE_AM_I, 'logo.png') 
     thumbnail = self.thumbnail_image(path) 
     self.image.set_from_pixbuf(thumbnail) 

     # Connect signals 
     self.builder.connect_signals(self) 

     # Everything is ready 
     self.window.show() 

    def main_quit(self, widget): 
     Gtk.main_quit() 

    def thumbnail_image(self, image): 
     """Creates a thumbnail GdkPixbuf of given image""" 

     # Create thumbnail 
     img = Image.open(image) 
     img.thumbnail((200, 300), Image.ANTIALIAS) 

     # Convert to GdkPixbuf 
     if img.mode != 'RGB':   # Fix IOError: cannot write mode P as PPM 
      img = img.convert('RGB') 
     buff = StringIO.StringIO() 
     img.save(buff, 'ppm') 
     contents = buff.getvalue() 
     buff.close() 
     loader = GdkPixbuf.PixbufLoader.new_with_type('pnm') 
     loader.write(contents) 
     pixbuf = loader.get_pixbuf() 
     loader.close() 

     return pixbuf 

if __name__ == '__main__': 
    gui = MyApp() 
    Gtk.main() 

tập tin Glade (test.glade):

<?xml version="1.0" encoding="UTF-8"?> 
<interface> 
    <!-- interface-requires gtk+ 3.0 --> 
    <object class="GtkWindow" id="window"> 
    <property name="can_focus">False</property> 
    <property name="window_position">center-always</property> 
    <property name="default_width">300</property> 
    <property name="default_height">200</property> 
    <signal name="destroy" handler="main_quit" swapped="no"/> 
    <child> 
     <object class="GtkImage" id="image"> 
     <property name="visible">True</property> 
     <property name="can_focus">False</property> 
     </object> 
    </child> 
    </object> 
</interface> 

gì nó trông giống như:

Result of converting PIL Image to GdkPixbuf

(Cải tiến để thêm Alpha hỗ trợ kênh được hoan nghênh)

Kind coi

Các vấn đề liên quan