将PIL图像转换为GTK Pixbuf [英] Converting PIL Image to GTK Pixbuf

查看:165
本文介绍了将PIL图像转换为GTK Pixbuf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在查看是否有另一种方法将PIL图像转换为GTK Pixbuf。
现在我所拥有的就是我发现并剽窃我的需求的低效编程实践。这是我到目前为止:

  def image2pixbuf(self,im):
file1 = StringIO.StringIO( )
im.save(file1,ppm)
contents = file1.getvalue()
file1.close()
loader = gtk.gdk.PixbufLoader(pnm )
loader.write(contents,len(contents))
pixbuf = loader.get_pixbuf()
loader.close()
return pixbuf

$ b

有没有更简单的方法来做这种转换,我错过了?

  import numpy $ b解决方案

如果您使用numpy数组, $ b arr = numpy.array(im)
return gtk.gdk.pixbuf_new_from_array(arr,gtk.gdk.COLORSPACE_RGB,8)


I am looking to see if there is another way to convert a PIL Image to GTK Pixbuf. Right now all I have is what seems to be like inefficient coding practice that I found and hacked to my needs. This is what I have so far:

def image2pixbuf(self,im):  
    file1 = StringIO.StringIO()  
    im.save(file1, "ppm")  
    contents = file1.getvalue()  
    file1.close()  
    loader = gtk.gdk.PixbufLoader("pnm")  
    loader.write(contents, len(contents))  
    pixbuf = loader.get_pixbuf()  
    loader.close()  
    return pixbuf 

Is there some easier way to do this conversion that I missed?

解决方案

You can do it efficiently if you go via a numpy array:

import numpy
arr = numpy.array(im)
return gtk.gdk.pixbuf_new_from_array(arr, gtk.gdk.COLORSPACE_RGB, 8)

这篇关于将PIL图像转换为GTK Pixbuf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆