Skip to content

Instantly share code, notes, and snippets.



Last active Jul 24, 2018
What would you like to do?
Script to download the latest DSCOVR image. Can probably be easily combined with for use with background-setters other than nitrogen.
#!/usr/bin/env python3
from io import BytesIO
from json import loads
from datetime import datetime, timedelta
from time import strptime
from subprocess import call
from os import makedirs
from os.path import expanduser, split
from urllib.request import urlopen
from PIL import Image, ImageDraw, ImageFont
# Path to the output file.
OUTPUT = expanduser("~/.config/nitrogen/earth-image")
# URL with JSON info about images. Expects date=YYYY-MM-DD.
JSON_URL = '{:04d}-{:02d}-{:02d}'
# URL to download images. Expects an image name parsed out of the JSON.
IMG_URL = '{}.jpg'
# The format of time strings in the JSON data (for strptime).
TIME_FORMAT = '%Y-%m-%d %H:%M:%S'
# The font to use to write the date.
FONT_FILE = '/usr/share/fonts/TTF/Hack-Regular.ttf'
# The font size to use to write the date.
def main():
# Get the JSON data. Work backward in time if there's nothing yet for today.
print('Getting JSON data...')
dt = 0
json = []
while len(json) < 1:
urldate = + timedelta(days = dt)
with urlopen(JSON_URL.format(urldate.year, urldate.month, as data:
json = loads('utf-8'))
dt -= 1
# Pick the most recent image.
print('Parsing JSON...')
newest_img = json[0]
for img in json:
if strptime(img['date'], TIME_FORMAT) > strptime(newest_img['date'], TIME_FORMAT):
newest_img = img
print('Latest image is from {}.'.format(newest_img['date']))
# Download the image file.
print('Downloading image...')
with urlopen(IMG_URL.format(newest_img['image'])) as imagedata:
bg =
# Add text with the current date and save.
print('Formatting image...')
w, h = bg.size
draw = ImageDraw.Draw(bg)
font = ImageFont.truetype(FONT_FILE, size = FONT_SIZE)
draw.text((0, h - FONT_SIZE * 2), newest_img['date'], font = font)
print('Saving image...')
makedirs(split(OUTPUT)[0], exist_ok=True), 'png')
# Set the background.
print('Setting background...')
call(["nitrogen", "--restore"])
if __name__ == "__main__":
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment