Converting PIL GdkPixbuf

前端 未结 3 619
时光说笑
时光说笑 2020-12-19 11:06

How can I convert PIL Image in pixbuf?. I tried to change many examples but no solution

import array
from gi.repository import GdkPixbuf

def image2pixbuf(se         


        
3条回答
  •  旧时难觅i
    2020-12-19 11:48

    This is how I did it in PyGtk, maybe this still works (I'm copying the code after pygi-convert'ed it):

    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 regards


    EDIT: Ok, this seems to be working... I just so hate PyGObject poor C ported API (until now...).

    Code (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()
    

    Glade file (test.glade):

    
    
      
      
        False
        center-always
        300
        200
        
        
          
            True
            False
          
        
      
    
    

    What it looks like:

    Result of converting PIL Image to GdkPixbuf

    (Improvements to add Alpha channel support are welcome)

    Kind regards

提交回复
热议问题