Skip to content

Instantly share code, notes, and snippets.

@quantum5
Created November 7, 2017 08:13
Show Gist options
  • Save quantum5/4aa0b5cdd7a753e828001e14532a44b9 to your computer and use it in GitHub Desktop.
Save quantum5/4aa0b5cdd7a753e828001e14532a44b9 to your computer and use it in GitHub Desktop.
Let WSL use synchronize with Windows backgrounds
#!/usr/bin/env python
from __future__ import division
import time
import os
import threading
import subprocess
from wand.image import Image
from watchdog.events import FileSystemEventHandler
from watchdog.observers import Observer
BACKGROUND = '/mnt/c/Users/Quantum/AppData/Roaming/Microsoft/Windows/Themes/TranscodedWallpaper'
RESULT = '/home/quantum/.config/i3/background.png'
WIDTH = 1600
HEIGHT = 860
REAL_HEIGHT = 900
background_dir = os.path.dirname(BACKGROUND)
debounce = threading.Event()
class EventHandler(FileSystemEventHandler):
def on_modified(self, event):
if event.src_path == BACKGROUND:
debounce.set()
def update_wallpaper():
with Image(filename=BACKGROUND) as img:
w, h = img.size
scale = max(WIDTH / w, REAL_HEIGHT / h)
left = (w * scale - WIDTH) / 2
top = (h * scale - REAL_HEIGHT * 2 + HEIGHT) / 2
img.resize(int(w * scale), int(h * scale))
img.crop(left=int(left), top=int(top), width=WIDTH, height=HEIGHT)
img.save(filename=RESULT)
subprocess.call(['feh', '--bg-fill', RESULT])
def main():
handler = EventHandler()
observer = Observer()
observer.schedule(handler, background_dir)
observer.start()
update_wallpaper()
try:
while True:
while not debounce.wait(86400):
pass
debounce.clear()
while debounce.wait(0.2):
debounce.clear()
update_wallpaper()
except KeyboardInterrupt:
observer.stop()
observer.join()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment