我正在努力将二进制数据放入gtk.gdk.pixbuf中。

这应该可以说明我的问题:

file = open("image.jpg", "rb")
//Ultimately this is going to come from a BLOB
binary = f.read()
//I created a pixbuf directly from the jpg
//and took the rowstride values &c. from that
pixbuf = gtk.gdk.pixbuf_new_from_data(
    binary, gtk.gdk.COLORSPACE_RGB, False, 8 , 450, 640, 1352)


失败的原因是:

ValueError: data length (90825) is less then required by the other parameters (865280)

我当前的解决方法是将二进制数据写入文件,然后从该文件创建Pixbuf。感觉太hacky了。我正在从数据库中读取二进制数据,因此我确实需要一个可以直接从缓冲区创建pixbuf的解决方案。

我不明白,如果我仅从数据库中读取BLOB并将其写入文件,然后将其作为pixbuf加载,却很难直接从缓冲区中读取,为什么这么简单!

我究竟做错了什么?

最佳答案

您正在尝试使用方法made for RGB-data only处理jpg-data。

如果要处理jpg,请按照建议的here格式使用加载程序:

file = open("image.jpg", "rb")
binary = file.read()
loader = gtk.gdk.PixbufLoader("jpeg")
loader.write(binary)
loader.close()
pixbuf = loader.get_pixbuf()


现在pixbuf包含您需要的内容。解决方法used such a loader automatically

关于python - 从二进制数据创建gtk.gdk.Pixbuf,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13140861/

10-12 03:18
查看更多