user2399377
user2399377

Reputation: 101

How to create a PixBuf from file with Gdk3?

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)

Upvotes: 10

Views: 8301

Answers (2)

luciomrx
luciomrx

Reputation: 25

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.

Upvotes: 1

Schcriher
Schcriher

Reputation: 943

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'

Upvotes: 26

Related Questions