How to create PixBuf from a file using Gdk3?

Wednesday: Python3

Libraries:

from gi.repository import Gtk, Gdk import cairo 

I want to create "pixbuf from a file", but this method no longer exists in Gdk3.

 pb = Gdk.pixbuf_new_from_file('sunshine.png') Gdk.cairo_set_source_pixbuf(cr, pb, 0, 0) 

Result in: AttributeError: object 'gi.repository.Gdk' does not have attribute 'pixbuf_new_from_file'

Does anyone know how to do this with Gdk3? Gdk3 seems to support only these methods:

 gdk_pixbuf_get_from_window gdk_pixbuf_get_from_surface 

Update

Found myself:

 image = Gtk.Image() image.set_from_file('sunshine.png') pixbuf = image.get_pixbuf() Gdk.cairo_set_source_pixbuf(cr, pixbuf, 10, 10) 
+6
source share
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'

+14
source

I don't know much about python, but in general, you should not use a gtk image directly to upload a file. Use GdkPixbuf instead because of error handling. The reason is that there is no (in python) attribute β€œpixbuf new from file”, because for most of the processing, GdkPixbuf is still used (in C) gdk-pixbuf-2.0. Why they do not spoil or still hold it, I have no idea. In python, you need to find the gdk pixbuf module instead of using GDK3, as Skriere noted.

+1
source

Source: https://habr.com/ru/post/945373/