Skip to content

Instantly share code, notes, and snippets.

Forked from stchris/
Created February 27, 2011 22:00
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
Gets the current image of the day from NASA and sets it as the background. The summary / description text is written to the image.
#!/usr/bin/env python
Gets the current image of the day from NASA and sets it as the
background. 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)
(in Mac OS X: sudo easy_install PIL && sudo easy_install feedparser)
Christian Stefanescu
Based on the bash script of Jessy Cowan-Sharp -
intelli_draw method from:
import urllib
import feedparser
import os
import commands
osx = os.uname()[0] == "Darwin"
has_pil = True
import Image
import ImageDraw
import ImageFont
except ImportError:
has_pil = False
print "One or more PIL modules missing, not overlaying description."
# Configurable settings
FONT_PATH = '/usr/share/fonts/truetype/ubuntu-font-family/Ubuntu-B.ttf'
if osx:
FONT_PATH = '/Library/Fonts/Georgia.ttf'
# how many empty text rows should be inserted to overcome top panel overlap
# Don't change stuff beyond this point
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.
font = ImageFont.truetype(FONT_PATH, FONT_SIZE)
img =
(img_width, img_height) = img.size
draw = ImageDraw.Draw(img)
lines, tmp, h = intelli_draw(draw, text, font, img_width)
yoffset = 0
if osx:
yoffset = 120
for i in lines:
draw.text((0, yoffset + 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)
if has_pil:
write_description(img_file, text)
if osx:
status = os.system("osascript -e 'tell app \"Finder\" to set desktop picture to (POSIX file \"%s\")'" % img_file);
status = set_gnome_wallpaper(img_file)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment