Last active
May 4, 2022 16:25
-
-
Save mijorus/f7990a05e291fb5bde4139f9ecf09825 to your computer and use it in GitHub Desktop.
Load a Gtk.Image from an http URL, with Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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