Environment: Python3
Libraries:
from gi.repository import Gtk, Gdk
import cairo
I want to create a 'pixbuf from file' but the method does not longer exist in Gdk3.
pb = Gdk.pixbuf_new_from_file('sunshine.png')
Gdk.cairo_set_source_pixbuf(cr, pb, 0, 0)
Result in: AttributeError: 'gi.repository.Gdk' object has no attribute 'pixbuf_new_from_file'
Does anybody know how to do this with Gdk3? Seems that Gdk3 only support these methods:
gdk_pixbuf_get_from_window
gdk_pixbuf_get_from_surface
Update
Found it out myself:
image = Gtk.Image()
image.set_from_file('sunshine.png')
pixbuf = image.get_pixbuf()
Gdk.cairo_set_source_pixbuf(cr, pixbuf, 10, 10)
The question is old, but maybe it helps someone:
from gi.repository import GdkPixbuf
pixbuf = GdkPixbuf.Pixbuf.new_from_file('sunshine.png')
Tested on GdkPixbuf._version == '2.0'
I dont know much about python, but in general you should not using gtk image directly to load file. Use GdkPixbuf instead because of error handling. The reason there is no attribute (in python) "pixbuf new from file", is because for much of the GdkPixbuf handling, still using (in C) gdk-pixbuf-2.0. Why they haven't port or still keeping it, I have no idea. In python you have to find the "gdk pixbuf" module instead of using GDK3, as Schcriher pointed out.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With