【问题标题】:Converting PIL GdkPixbuf转换 PIL GdkPixbuf
【发布时间】:2012-05-07 16:35:47
【问题描述】:

如何在 pixbuf 中转换 PIL 图像?我尝试更改许多示例但没有解决方案

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)

错误

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

我使用 Pygobject https://live.gnome.org/PyGObject

【问题讨论】:

    标签: python python-imaging-library pygobject gdkpixbuf


    【解决方案1】:

    我在使用 Gtk3 时遇到了同样的问题。 GdkPixbuf.PixbufDestroyNotify - NotImplementedError - python gtk3

    似乎有一个错误。我已经举报了。 https://bugzilla.gnome.org/show_bug.cgi?id=674691

    Gtk3 有这个错误。但是你仍然可以在 Gtk2 中做到这一点。

    【讨论】:

    • 错误报告中的这条评论可能有用:“所以你仍然可以使用new_from_file(),或者只使用new(),稍后再设置数据”
    • 是的,我看到了。但我特别想要 gdkPixbuf new_from_data()
    【解决方案2】:

    这就是我在 PyGtk 中的做法,也许这仍然有效(我在 pygi 转换后复制代码):

    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
    

    亲切的问候


    编辑:好的,这似乎工作......我只是非常讨厌 PyGObject 糟糕的 C 移植 API(直到现在......)。

    代码(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 文件(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>
    

    它的样子:

    (欢迎改进以添加 Alpha 通道支持)

    亲切的问候

    【讨论】:

      猜你喜欢
      • 2016-12-27
      • 2017-08-23
      • 2015-04-30
      • 2012-05-03
      • 1970-01-01
      • 1970-01-01
      • 2011-02-14
      • 1970-01-01
      • 2017-01-24
      相关资源
      最近更新 更多