简体   繁体   English

python GTK3 PIL jpg格式的图像

[英]python GTK3 Image from PIL jpg format

I'm trying to open image (jpg) from the buffer (get as blob from Oracle database) as Gtk.Image() and add it to a Gtk window, but I get error "Expected Gtk.Widget, but got PIL.JpegImagePlugin.JpegImageFile". 我正在尝试从缓冲区(从Oracle数据库获取blob)作为Gtk.Image()打开图像(jpg),并将其添加到Gtk窗口,但出现错误“ Expt Gtk.Widget,但得到了PIL.JpegImagePlugin .JpegImageFile”。 I can show the image with show(), I can window.add jpeg file from path on the disc, and then show the window, but when I try to add jpg from buffer I get the error. 我可以用show()显示图像,也可以从光盘上的path窗口添加window.add jpeg文件,然后显示窗口,但是当我尝试从缓冲区添加jpg时出现错误。 Here is what I produced: 这是我制作的:

my_source=Here_I_get_BLOB_img_from_database()
newimage=Gtk.Image()
newimage=my_source.read()
image=io.BytesIO(newimage)
dt=Image.open(image)
newwindow = Gtk.Window(modal=True)

In this point actually I have jpg in buffer and I can do: 在这一点上,实际上我在缓冲区中有jpg,我可以这样做:

dt.show() # to display the image in system imageviewer

Or save dt as jpg, or add to newwindow a result of image.set_from_file("path with jpg extension") but don't know how to do this: 或将dt另存为jpg,或将image.set_from_file(“具有jpg扩展名的路径”)的结果添加到新窗口中,但不知道如何执行此操作:

newwindow.add(dt) 

Or anything with similar effect. 或任何具有类似效果的东西。 How to do this in simplest way? 如何以最简单的方式做到这一点?

What worked for me was - 对我有用的是-

Gtk.Image to load img from buffer has Pixbuf object, which for example can be loaded from stream. 从缓冲区加载img的Gtk.Image具有Pixbuf对象,例如可以从流中加载。 So: 所以:

from gi.repository import (...) GdkPixbuf, GLib, Gio
(...)
my_source=Here_I_get_BLOB_img_from_database()
newimage=my_source.read()
glib=GLib.Bytes.new(newimage)
stream = Gio.MemoryInputStream.new_from_bytes(glib)
pixbuf = GdkPixbuf.Pixbuf.new_from_stream(stream, None)
image=Gtk.Image().new_from_pixbuf(pixbuf)
my_window = Gtk.Window(modal=True, title="Image")
my_window.add(image)
image.show()
my_window.show()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM