i3: how to make a pretty lock screen with a small bash script

My regular readers already knows that my favorite desktop environment on Linux is i3.
However, let’s face it, the UI of the default theme of lockscreen tool (i3lock) is hawful.

Luckily, i3lock provides some command line options, such us the -i option, that allows the customization of lockscreen background.

Using a tiny bash script is possible to make a blurried/pixelate screenshot of the current desktop with a padlock in the center, and using it as i3lock background.

The script has only one pre-requisite: imagemagick need to be installed on the box.

$ sudo apt install imagemagick

So, with the import tool (from imagemagick) we can create a screenshot of the current screen:

import -window root screenshot.png 

Then, using the convert tool, we apply some modification on the image (in this example, a pixelation or a blurring):

convert  screenshot.png  -scale 10% -scale 1000%  screenshot.png 

or

convert  screenshot.png  -blur 2,5  screenshot.png 

I prefer the pixelation, due the speed of the process against the blurring process.

Finally, using again the convert tool, we apply a centered padlock image (like this) on the screenshot:

convert screenshot.png lock.png -gravity center -composite screenshot.png 

Here the result:

The generated image can therefore used as background for i3lock:

i3lock -u -i screenshot.png

Let’s assemble the bash script

Let’s collect all commands in a single script, that stores the screenshot in a temporary file:

#!/bin/bash
icon="$HOME/.config/i3/lock.png"
# create a temp file
img=$(mktemp /tmp/XXXXXXXXXX.png)
# Take a screenshot of current desktop
import -window root $img 
# Pixelate the screenshot
convert $img -scale 10% -scale 1000% $img
# Alternatively, blur the screenshot (slow!)
#convert $img -blur 2,5 $img
# Add the lock image
convert $img $icon -gravity center -composite $img
# Run i3lock with custom background
i3lock -u -i $img
# Remove the tmp file
rm $img

The script can be saved in the user local directory, and called automatically when screen goes to sleep using xss-lock, adding this line at the end of i3’s config file:

 exec --no-startup-id xss-lock -l [path of your script]

(I prefer this solution to the use of systemd because it is more reliable and compatible also with systems that do not use systemd)

Related posts

  1. Cybersecurity Roundup #15
  2. Why Huawei USB stick setup on linux adds a strange “Huawei Autorun” script in system start?
  3. LinuxCheck: Linux information gathering tool
  4. Quick mount of iOS Apps documents on Linux, using iFuse and bash
  5. Full Disk Encryption: tools and setup suggestion for personal data protection