Use Gdk.Pixbuf.new_from_file_at_scale() along with Gtk.Image.set_from_pixbuf():
pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_scale(f.name, width=220, height=220,
preserve_aspect_ratio=False)
self.edit_tour_meetingpoint_image.set_from_pixbuf(pixbuf)
If you want to preserve the aspect, just set that argument to True or use: GdkPixbuf.Pixbuf.new_from_file_at_size(f.name, width=220, height=220)
Side note: The reason you need to call read() before using the file is because it is buffered and hasn't been written to disk. The read call is causing the buffer to flush, a clearer technique (from a readability standpoint) would be to call flush() instead of read().
If you want to get rid of the temporary file, use the Gio module along with a streaming pixbuf:
from gi.repository import Gtk, GdkPixbuf, Gio
file = Gio.File.new_for_uri('http://www.gnome.org/wp-content/themes/gnome-grass/images/gnome-logo.png')
pixbuf = GdkPixbuf.Pixbuf.new_from_stream_at_scale(file.read(cancellable=None),
width=220, height=220,
preserve_aspect_ratio=False,
cancellable=None)
self.edit_tour_meetingpoint_image.set_from_pixbuf(pixbuf)
You can take things further by using an async image stream which then injects the completed results into the app when the pixbuf is ready, maintaining interactivity in the UI during the file transfer:
from gi.repository import Gtk, GdkPixbuf, Gio
# standin image until our remote image is loaded, this can also be set in Glade
image = Gtk.Image.new_from_icon_name('image-missing', Gtk.IconSize.DIALOG)
def on_image_loaded(source, async_res, user_data):
pixbuf = GdkPixbuf.Pixbuf.new_from_stream_finish(async_res)
image.set_from_pixbuf(pixbuf)
file = Gio.File.new_for_uri('http://www.gnome.org/wp-content/themes/gnome-grass/images/gnome-logo.png')
GdkPixbuf.Pixbuf.new_from_stream_at_scale_async(file.read(cancellable=None),
220, 220, # width and height
False, # preserve_aspect_ratio
None, # cancellable
on_image_loaded, # callback,
None) # user_data
Note we cannot use the nice keyword arguments in the async version because of the user_data arg. This goes away in pygobject 3.12 where the user_data can actually be left off if not used (or used as a keyword arg too).