Bing wallpapers on Linux (python version)

Earlier I made a post with the script which pulls wallpapers from Bing. Here I show my Python script, which is doing the same:

  • Saves new image from the Bing server.
  • Adds watermark with the copyright information to it.
  • Sets this image the lock screen tool and/or as a desktop wallpaper.

Script logic

  1. To store wallpapers I use ~/wallpapers/bing/ folder.
  2. In order to detect if image on the Bing server is the new one, I do the following:
    • I save copyright information of the last saved (and currently used) image in the ~/wallpapers/bing/bing_wallpaper_copyright.txt file.
    • On each request to Bing I compare copyright information of the image on server with the one in the file.
    • If copyright is the same - then I don’t update image.
    • If copyright differs - then I download the image.
    • Every image with the copyright watermark I save in the ~/wallpapers/bing/ folder.
      • This is not necessary to do. If you want - you can omit this step.

        Image with copyright watermark

  3. Then I use downloaded image as:
    • As a background picture for the [slick-greeter] for LightDM.
      • Because slick-greeter has panel on top, I use image without copyright watermark for it.
      • For that I am saving it under the /mnt/data/greeter-wallpapers/bing_image.jpg name.
      • This image is always overwritten with the latest downloaded one. I do not save images without copyright watermark.
    • Lock screen picture for betterlockscreen.
      • Picture with the watermark copyright on it.
    • Lock screen picture for slock-bgimage:
      • I take picture with the copyright watermark on it as a source and I add small locker icon to its corner. Source www.flaticon.com.

        Lock icon for watermark

      • Then I save this image under ~/wallpapers/bing/current_lock_image.jpg name.

        Image with watermark and lock

I am not using /tmp folder for storing my images for screen locker and for my greeter program. I do it because locker and greeter shall work properly after reboot.

I stopped using betterlockscreen because it leaks pop up and notification windows in my setup. I tried to fix it but I could not find solution. And I switched to slock which is much simpler, faster and has no issues with leaking windows.

Python script itself

Expand to see full script

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#!/bin/python

import requests
import json
import sys
import os
from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont

# Defining URL for pulling the data
bing_url="https://www.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1&mkt=de-DE"

# Path to the text file with the copyright text
local_copyright_file = '/home/alexgum/wallpapers/bing/bing_wallpaper_copyright.txt'

# Path to the font I am using
local_font = '/usr/share/fonts/TTF/Arial.TTF'

# Folder where I store wallpapers
wallpaper_folder = '/home/alexgum/wallpapers/bing/'

# Path to the temporary file
# Firstly, image is downloaded there
temp_file_path = '/tmp/bing_image_temp.jpg'

# Path to the final file which is going to be used by slick-greeter
greeter_file_path = '/mnt/data/greeter-wallpapers/bing_image.jpg'

# Icon I use to add locker watermark on the image for slock
lock_logo_file = '/home/alexgum/.config/icons/lock_64.png'

# Path to the image for slock
slock_file = '/home/alexgum/wallpapers/bing/current_lock_image.jpg'

# Function to extract file name from the JSON data
def make_img_file_name(full_json: dict) -> str:
    try:
        url_temp = full_json['images'][0]['url'].split('&')
        return url_temp[0].split('.')[1] + '.jpg'
    except Exception as e:
        print("make_img_file_name(): Error. Cannot produce file name.")
        print(e)
        sys.exit(1)

# Function to get URL for downloading picture
# Input - full JSON response, returns string with URL
def get_picture_url_from_json(full_json: dict) -> str:
    try:
        return 'https://bing.com/' + full_json['images'][0]['url']
    except Exception as e:
        print("get_picture_url(): Error. Cannot get picture url.")
        print(e)
        sys.exit(1)

# Function to get copyright information from the file on disk
# Input - path to a file with the copyright data of the current image
# Returns string with the copyright text, or "None"
def get_copyright_from_file(local_file_name: str) -> str:
    try:
        with open(local_file_name, 'rt') as f:
            return f.readline()
    except Exception:
        return "None"

# Function to get copyright information from the JSON data
# Input - full JSON response, returns string with the copyright text
def get_copyright_from_json(full_json: dict) -> str:
    try:
        return full_json['images'][0]['copyright']
    except Exception as e:
        print("get_copyright_from_json(): Error. Cannot get copyright from url.")
        print(e)
        sys.exit(1)

# Function to download image
# Input - URL and path to file where image to be stored
def img_download(url: str,
                 target_file: str = '/tmp/bing_image_temp.jpg') -> None:
    try:
        image_file = requests.get(url)
        with open(target_file, 'wb') as f:
            f.write(image_file.content)
    except Exception as e:
        print(e)
        sys.exit(1)

# Function to make wallper image with the copyright watermark
def make_new_wallpaper(source_file_path: str,
                       target_wallpaper_name: str,
                       copyright_text: str,
                       font_path: str = '/usr/share/fonts/TTF/Arial.TTF') -> None:

    # Code is based on this blog post:
    ##   https://betterprogramming.pub/add-copyright-or-watermark-to-photos-using-python-a3773c71d431

    # Preparing image for drawing (open canvas)
    img = Image.open(source_file_path)
    w, h = img.size

    # Preparing text for drawing
    ## Making drawing canvas
    drawing = ImageDraw.Draw(img)
    
    ## Creating text image
    cprw_font = ImageFont.truetype(font_path, 14)
    copyright_text_with_margins = " " + copyright_text + " "

    ## Identifying its dimensions
    text_w, text_h = drawing.textsize(copyright_text_with_margins,cprw_font)

    # Drawing new image from the text object
    c_text = Image.new('RGBA', (text_w, text_h), color = '#000000')
    drawing = ImageDraw.Draw(c_text)
    drawing.text((0,0),copyright_text_with_margins,fill = '#FFFFFF',font = cprw_font)
    # Adding Alpha channel value
    c_text.putalpha(130)

    # Identifying text position on the target picture
    # 10 pixels from top and 10 pixels from the right edge
    text_position = (w - text_w - 10), 10

    # Pasting this object into the image
    img.paste(c_text,text_position,mask=c_text)

    # Saving new image
    try:
        img.save(target_wallpaper_name)
    except Exception as e:
        print('make_new_wallpaper() function cannot save new picture.')
        print(e)
        sys.exit(1)

def add_logo(source_file_path: str,
             target_lock_file: str,
             logo: str) -> None:

    # Preparing image for drawing (open canvas)
    img = Image.open(source_file_path)
    w, h = img.size
    logo_img = Image.open(logo).convert('RGBA')
    logo_w, logo_h = logo_img.size

    # converting logo background to transparent and adding alpha channel 178 (80%) to black
    newImage = []
    for item in logo_img.getdata():
        if item[:3] == (255, 255, 255):
            newImage.append((255, 255, 255, 0))
        else:
            newImage.append((0,0,0,178))
    logo_img.putdata(newImage)
    logo_position = 10, (h - logo_h - 16)
    img.paste(logo_img,logo_position,mask=logo_img)

    # Saving new image
    try:
        img.save(target_lock_file)
    except Exception as e:
        print('add_logo() function cannot save new picture.')
        print(e)
        sys.exit(1)

# Main function to assemble it all
def main() -> None:
    try:
        # Getting the JSON data
        json_data = requests.get(bing_url).json()

        # Extracting new copyright text
        new_copyright_text = get_copyright_from_json(json_data)

        # Comparing the new copyright text with the stored in the file
        # If texts are different, then go on
        if get_copyright_from_file(local_copyright_file) != new_copyright_text:

            # Extracting new image file name from JSON
            file_name = wallpaper_folder + make_img_file_name(json_data)

            # Extracting URL for image download
            file_url = get_picture_url_from_json(json_data)

            # Downloading the image and saving it in the /tmp folder
            img_download(file_url, temp_file_path)

            # Creating wallpaper image with the copyright watermark
            # and saving it in the ~/wallpapers/bing/ folder
            make_new_wallpaper(temp_file_path, file_name, new_copyright_text, local_font)

            # Updating copyright information in the local file
            open(local_copyright_file,'wt').write(new_copyright_text)

            # Adding locker logo to make image for slock
            # this image is stored in the ~/wallpapers/bing/current_lock_image.jpg
            add_logo(file_name, slock_file, lock_logo_file)

            # Copy orignal image from /tmp to the /mnt/data/greeter-wallpapers/bing_image.jpg
            # for being used with slick-greeter
            os.system('cp ' + temp_file_path + ' ' + greeter_file_path)

            # Setting up betterlockscreen 
            ## Optional part I don't use anymore. 
            os.system('DISPLAY=:0 /usr/bin/betterlockscreen -u ' + file_name)
            
    except Exception as e:
        print(e)

if __name__ == '__main__':
    main()

Runnig the script

Use simple cronjob:

1
2
# {min} {hour} {day of month} {moth} {day of week}  command to be executed
0,30 6-19 * * * /home/alexgum/.scripts/lockscreenpicture.py > /tmp/lockscreen.log