Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)
like image 277
user2399377 Avatar asked May 19 '13 17:05

user2399377


2 Answers

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'

like image 172
Schcriher Avatar answered Nov 05 '22 10:11

Schcriher


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.

like image 1
luciomrx Avatar answered Nov 05 '22 08:11

luciomrx