wallpaper_merger.py 2.31 KB
Newer Older
1
from gi.repository import Gio
Gabriele Musco's avatar
Gabriele Musco committed
2
3
from PIL import Image
from PIL.ImageOps import fit
4
from os import environ as Env
Gabriele Musco's avatar
Gabriele Musco committed
5
from subprocess import run
6
import re
7
8
9

TMP_DIR = '/tmp/HydraPaper/'
SWAY_CONF_PATH = f'{Env.get("HOME")}/.config/sway/config'
Gabriele Musco's avatar
Gabriele Musco committed
10
11


12
def multi_setup_pillow(monitors, save_path, wp_setter_func=None):
Gabriele Musco's avatar
Gabriele Musco committed
13
    images = list(map(Image.open, [m.wallpaper for m in monitors]))
14
15
16
    resolutions = [
        (m.width * m.scaling, m.height * m.scaling) for m in monitors
    ]
17
    offsets = [(m.offset_x, m.offset_y) for m in monitors]
Gabriele Musco's avatar
Gabriele Musco committed
18

19
20
21
    # DEBUG
    # for m in monitors:
    #     print(m)
22

23
24
25
26
27
28
    final_image_width = max([
        m.offset_x + m.width * m.scaling for m in monitors
    ])
    final_image_height = max([
        m.offset_y + m.height * m.scaling for m in monitors
    ])
29

Gabriele Musco's avatar
Gabriele Musco committed
30
31
32
    n_images = []
    for i, r in zip(images, resolutions):
        n_images.append(fit(i, r, method=Image.LANCZOS))
33
    final_image = Image.new('RGB', (final_image_width, final_image_height))
Gabriele Musco's avatar
Gabriele Musco committed
34
35
36
    for i, o in zip(n_images, offsets):
        final_image.paste(i, o)
    final_image.save(save_path)
37

38
39

def set_wallpaper_gnome(path, wp_mode='spanned', lockscreen=False):
40
41
42
43
    gsettings_path = 'org.gnome.desktop.background'
    if lockscreen:
        gsettings_path = 'org.gnome.desktop.screensaver'
    gsettings = Gio.Settings.new(gsettings_path)
44
45
46
    wp_key = 'picture-uri'
    mode_key = 'picture-options'
    gsettings.set_string(wp_key, 'file://{}'.format(path))
Gabriele Musco's avatar
Gabriele Musco committed
47
    gsettings.set_string(mode_key, wp_mode)
48

49
50

def set_wallpaper_mate(path, wp_mode='spanned', lockscreen=False):
51
52
53
    if lockscreen:
        print('Lock screen wallpaper on MATE unsupported')
        return
54
55
56
57
58
    gsettings = Gio.Settings.new('org.mate.background')
    wp_key = 'picture-filename'
    mode_key = 'picture-options'
    gsettings.set_string(wp_key, path)
    gsettings.set_string(mode_key, wp_mode)
59
60


61
62
63
64
65
66
67
def set_wallpaper_sway(monitors, lockscreen=False):
    if lockscreen:
        print('Lock screen wallpaper on sway unsupported')
        return
    with open(SWAY_CONF_PATH) as fd:
        conf = fd.read()
        fd.close()
68
    n_conf = re.sub(r'output .* bg .*', '', conf).strip()
69
70
71
72
73
74
    n_conf += '\n' + '\n'.join([
        f'output {m.name} bg {m.wallpaper} fill' for m in monitors
    ])
    with open(SWAY_CONF_PATH, 'w') as fd:
        fd.write(n_conf)
        fd.close()
Gabriele Musco's avatar
Gabriele Musco committed
75
    run('sway reload'.split(' '))