Skip to content

Instantly share code, notes, and snippets.

@febnug
Created November 21, 2026 04:58
Show Gist options
  • Select an option

  • Save febnug/d869f1ca77fc435f60c3bc4bb8ce6485 to your computer and use it in GitHub Desktop.

Select an option

Save febnug/d869f1ca77fc435f60c3bc4bb8ce6485 to your computer and use it in GitHub Desktop.
#!/usr/bin/python3
import os
import sys
import struct
import tempfile
DEST_DIR = "/var/lib/lightdm/backgrounds"
DEST_FILE = os.path.join(DEST_DIR, "current-wallpaper.png")
MAX_SIZE = 30 * 1024 * 1024
MAX_DIMENSION = 8192
def fail(message):
print(f"blankon-wallpaper-sync: {message}", file=sys.stderr)
sys.exit(1)
def main():
data = sys.stdin.buffer.read(MAX_SIZE + 1)
if not data:
fail("empty input")
if len(data) > MAX_SIZE:
fail("image exceeds 30 MiB limit")
if len(data) < 24 or data[:8] != b"\x89PNG\r\n\x1a\n":
fail("input is not a valid PNG")
if data[12:16] != b"IHDR":
fail("PNG IHDR chunk missing")
width, height = struct.unpack(">II", data[16:24])
if not (1 <= width <= MAX_DIMENSION):
fail("invalid image width")
if not (1 <= height <= MAX_DIMENSION):
fail("invalid image height")
os.makedirs(DEST_DIR, mode=0o750, exist_ok=True)
fd, temp_path = tempfile.mkstemp(
prefix=".wallpaper-",
suffix=".png",
dir=DEST_DIR
)
try:
with os.fdopen(fd, "wb") as output:
output.write(data)
output.flush()
os.fsync(output.fileno())
os.chown(temp_path, 105, 105)
os.chmod(temp_path, 0o644)
os.replace(temp_path, DEST_FILE)
dir_fd = os.open(DEST_DIR, os.O_DIRECTORY)
try:
os.fsync(dir_fd)
finally:
os.close(dir_fd)
except Exception:
try:
os.unlink(temp_path)
except FileNotFoundError:
pass
raise
print(f"Wallpaper updated: {width}x{height}")
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment