Last active
May 5, 2024 17:07
-
-
Save thiagokokada/aec7579b7a4074b09d32e8c5f3639d95 to your computer and use it in GitHub Desktop.
Lock screen using i3lock with background blur in 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
#!/usr/bin/env python | |
from subprocess import check_call, CalledProcessError | |
from tempfile import NamedTemporaryFile | |
from dpms import DPMS | |
from mss import mss | |
from PIL import Image, ImageFilter | |
GAUSSIAN_BLUR_RADIUS = 5 | |
SCREEN_TIMEOUT = (5, 5, 5) # Standby, Suspend, Off | |
# Get current DPMS settings | |
dpms = DPMS() | |
current_timeouts = dpms.GetTimeouts() | |
with mss() as sct: | |
# Get the "All-in-one" monitor | |
monitor = sct.monitors[0] | |
# Get raw pixels of the screen | |
sct_img = sct.grab(monitor) | |
# Create Image object using Pillow | |
img = Image.frombytes("RGB", sct_img.size, sct_img.rgb) | |
with NamedTemporaryFile(suffix=".png") as tempfile: | |
# Apply filters to Image | |
img = img.filter(ImageFilter.GaussianBlur(radius=GAUSSIAN_BLUR_RADIUS)) | |
# Save temporary file | |
img.save(tempfile.name, optimize=False, compress_level=1) | |
# Set monitor timeout to SCREEN_TIMEOUT | |
dpms.SetTimeouts(*SCREEN_TIMEOUT) | |
try: | |
# Load image in i3lock | |
check_call(["i3lock", "-nei", tempfile.name]) | |
except CalledProcessError: | |
# Something went wrong, lock it anyway | |
check_call(["i3lock", "-ne"]) | |
finally: | |
# Restore DPMS settings | |
dpms.SetTimeouts(*current_timeouts) |
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
-e git+https://github.com/m45t3r/python-dpms#egg=dpms | |
mss==3.0.1 | |
olefile==0.44 | |
Pillow-SIMD==4.3.0.post0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment