Bing Image Retrieval

A few years ago, when learning Python, I wrote a little script that will retrieve and save to disk the current background image. The directory where the image is saved is the first argument. You can run it via cron if you use a Linux based OS or via Windows Task Scheduler if you use, well, Windows.

I did notice that if you access from a USA IP address then you have the ability to retrieve a higher resolution 1920x1200 image, whereas from the UK (and likely elsewhere) you can only access the 1366x768 image.

import re
import urllib2
import os
import shutil
import sys

bing_url = ''
save_location = sys.argv[1]

p = re.compile('/az/.+?(jpg)')
resp = urllib2.urlopen(bing_url).read()

image_url =
if '_EN-US' in image_url:
res = image_url.split('_')
extension = image_url.split('.')[-1]
image_url = '_'.join(res[:-1]) + '_1920x1200.' + extension

img = urllib2.urlopen(bing_url + image_url)
except urllib2.HTTPError as e:
print e

image_name = image_url.split('/')[-1]

if not os.path.exists(save_location):
with open(save_location + image_name, 'wb') as f:
shutil.copyfileobj(img, f)

Edit: I have since updated this script with some nice new changes. You can find that post here:
This was posted on Mon 23 Jun 2014 (5 years ago) by Ryan McConville