Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Improved version of - gets the 'image of the day' from NASA, writes the summary onto the image and sets it as the Gnome wallpaper.
#!/usr/bin/env python
Gets the current image of the day from NASA and sets it as the
background in Gnome. The summary / description text is written
to the image.
PIL (apt-get install python-imaging or pip install PIL)
feedparser (apt-get install python-feedparser or pip install feedparser)
Christian Stefanescu
Based on the bash script of Jessy Cowan-Sharp -
intelli_draw method from:
import Image
import ImageDraw
import ImageFont
import urllib
import feedparser
import os
import commands
# Configurable settings
DOWNLOAD_FOLDER = '~/.backgrounds/'
FONT_PATH = '/usr/share/fonts/truetype/ubuntu-font-family/Ubuntu-B.ttf'
# how many empty text rows should be inserted to overcome top panel overlap
# Don't change stuff beyond this point
font = ImageFont.truetype(FONT_PATH, FONT_SIZE)
def get_latest_entry():
Get URL and description of the latest entry in the feed
feed = feedparser.parse(FEED_URL)
return (feed.entries[0].enclosures[0].href, feed.entries[0].summary)
def download_file(url):
Get the latest NASA image of the day from the feed, returns the name
of the downloaded file.
remote_file = urllib.urlopen(url)
local_name = url.split('/')[-1]
local_path = os.path.expanduser(os.path.join(DOWNLOAD_FOLDER, local_name))
local_file = open(local_path, 'w')
return local_path
def intelli_draw(drawer, text, font, containerWidth):
Figures out how many lines (and at which height in px) are needed to print
the given text with the given font on an image with the given size.
words = text.split()
lines = []
finished = False
line = 0
while not finished:
thistext = lines[line]
newline = []
innerFinished = False
while not innerFinished:
if drawer.textsize(' '.join(thistext), font)[0] > containerWidth:
newline.insert(0, thistext.pop(-1))
innerFinished = True
if len(newline) > 0:
line = line + 1
finished = True
tmp = []
for i in lines:
tmp.append(' '.join(i))
lines = tmp
(width, height) = drawer.textsize(lines[0], font)
return (lines, width, height)
def write_description(img_file, text):
Write the given text to the given imagefile and overwrite it.
img =
(img_width, img_height) = img.size
draw = ImageDraw.Draw(img)
lines, tmp, h = intelli_draw(draw, text, font, img_width)
for i in lines:
draw.text((0, 0 + j * h), i, font=font)
j = j + 1, 'w'), 'JPEG')
def set_gnome_wallpaper(file_path):
command = "gconftool-2 --set \
/desktop/gnome/background/picture_filename \
--type string '%s'" % file_path
status, output = commands.getstatusoutput(command)
return status
if __name__ == '__main__':
if not os.path.exists(os.path.expanduser(DOWNLOAD_FOLDER)):
(url, text) = get_latest_entry()
img_file = download_file(url)
write_description(img_file, text)
status = set_gnome_wallpaper(img_file)

afcuttin commented Mar 2, 2011

Unfortunately the script doesn't work for me (Ubuntu 10.04). This is what I get:

Traceback (most recent call last):
File "scripts/", line 40, in
font = ImageFont.truetype(FONT_PATH, FONT_SIZE)
File "/usr/lib/python2.6/dist-packages/PIL/", line 218, in truetype
return FreeTypeFont(filename, size, index, encoding)
File "/usr/lib/python2.6/dist-packages/PIL/", line 134, in init
self.font = core.getfont(file, size, index, encoding)
IOError: cannot open resource


stchris commented Mar 2, 2011


the FONT_PATH variable in the script should point to a font you have on your system. Pick one which really exists on your system, probably you can find fonts in /usr/share/fonts assuming you're using some unix-based OS.

behnam commented Mar 2, 2011

@afcuttin: Install ttf-ubuntu-font-family package:
$ sudo apt-get install ttf-ubuntu-font-family

I have a dual monitor setup with twinview (NVidia) and the picture in halfway in between the two screens, so the left half of my first screen is empty (purple-ish) and the right half of my right screen is empty


stchris commented Mar 3, 2011

@kroq-gar78: Have you tried changing the 'Style' setting in the Background preferences screen?

afcuttin commented Mar 6, 2011

After having installed the Ubuntu font, it worked. However, the size of the images from NASA is always different, so that the caption isn't always in the same place.
I got back to the original script, and added a conky panel on my desktop.

fndtn357 commented Jan 7, 2013

NameError: global name 'drawer' is not defined on Ubuntu 12.04. Is there something I am missing?

A-Genva commented Aug 7, 2016

After running the script, no change in the background image. --
No error in the terminal. -

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment