Skip to content

Instantly share code, notes, and snippets.

@mijorus
Last active May 4, 2022 16:25
Show Gist options
  • Save mijorus/f7990a05e291fb5bde4139f9ecf09825 to your computer and use it in GitHub Desktop.
Save mijorus/f7990a05e291fb5bde4139f9ecf09825 to your computer and use it in GitHub Desktop.
Load a Gtk.Image from an http URL, with Python
import requests
from gi.repository import Gtk, Adw, GdkPixbuf, GLib
def gtk_image_from_url(url: str, image: Gtk.Image):
"""Using the requests module we load an image from a http endpoint, then we can create a Pixbuf loader to load our image"""
response = requests.get(url)
response.raise_for_status()
loader = GdkPixbuf.PixbufLoader()
loader.write_bytes(GLib.Bytes.new(response.content))
loader.close()
image.set_from_pixbuf(loader.get_pixbuf())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment