From 921c368078e6bebb95950afbda145470d74c30c0 Mon Sep 17 00:00:00 2001 From: Martin Reuter Date: Wed, 25 Feb 2026 16:47:01 +0100 Subject: [PATCH] wrap glfw init to avoid warnings in headless mode and display animated gif --- tutorials/whippersnappy_tutorial.ipynb | 2 +- whippersnappy/gl/utils.py | 7 +++++-- whippersnappy/snap.py | 23 +++++++++++++---------- 3 files changed, 19 insertions(+), 13 deletions(-) diff --git a/tutorials/whippersnappy_tutorial.ipynb b/tutorials/whippersnappy_tutorial.ipynb index 5898a06..f28b561 100644 --- a/tutorials/whippersnappy_tutorial.ipynb +++ b/tutorials/whippersnappy_tutorial.ipynb @@ -286,7 +286,7 @@ }, "outputs": [], "source": [ - "Image(filename=outpath_gif)\n" + "display(Image(filename=outpath_gif))\n" ] } ], diff --git a/whippersnappy/gl/utils.py b/whippersnappy/gl/utils.py index 288e383..b24bd08 100644 --- a/whippersnappy/gl/utils.py +++ b/whippersnappy/gl/utils.py @@ -6,6 +6,7 @@ import logging import os import sys +import warnings from typing import Any import glfw @@ -181,8 +182,10 @@ def init_window(width, height, title="PyOpenGL", visible=True): window or False GLFW window handle on success, or False on failure. """ - if not glfw.init(): - return False + with warnings.catch_warnings(): + warnings.simplefilter("ignore") + if not glfw.init(): + return False glfw.window_hint(glfw.CONTEXT_VERSION_MAJOR, 3) glfw.window_hint(glfw.CONTEXT_VERSION_MINOR, 3) diff --git a/whippersnappy/snap.py b/whippersnappy/snap.py index 176d98e..f3dbcd2 100644 --- a/whippersnappy/snap.py +++ b/whippersnappy/snap.py @@ -2,6 +2,7 @@ import logging import os +import warnings import glfw import numpy as np @@ -151,16 +152,18 @@ def snap1( ref_height = 500 ui_scale = min(width / ref_width, height / ref_height) try: - if glfw.init(): - primary_monitor = glfw.get_primary_monitor() - if primary_monitor: - mode = glfw.get_video_mode(primary_monitor) - if width > mode.size.width: - logger.info("Requested width %d exceeds screen width %d, expect black bars", - width, mode.size.width) - elif height > mode.size.height: - logger.info("Requested height %d exceeds screen height %d, expect black bars", - height, mode.size.height) + with warnings.catch_warnings(): + warnings.simplefilter("ignore") + if glfw.init(): + primary_monitor = glfw.get_primary_monitor() + if primary_monitor: + mode = glfw.get_video_mode(primary_monitor) + if width > mode.size.width: + logger.info("Requested width %d exceeds screen width %d, expect black bars", + width, mode.size.width) + elif height > mode.size.height: + logger.info("Requested height %d exceeds screen height %d, expect black bars", + height, mode.size.height) except Exception: pass # headless — no monitor info available, that's fine