Skip to content

Instantly share code, notes, and snippets.

Last active January 19, 2022 13:21
Show Gist options
  • Save jmiserez/3bd58d8ee3c27f24dd6f to your computer and use it in GitHub Desktop.
Save jmiserez/3bd58d8ee3c27f24dd6f to your computer and use it in GitHub Desktop.
Linux / WSL alternative client for Bing Wallpaper, with annotation using ImageMagick
# exit on error
set -e
# Description:
# - Linux-compatible alternative to the official Bing Wallpaper client (
# - Downloads the current Bing wallpaper of the day directly.
# - Fetches the matching image description / copyright information and overlays it onto the image using ImageMagick.
# Usage:
# 1. Adjust paths saveDir, target1, target2.
# 2. Set wallpaper to update automatically:
# - Windows (Ubuntu WSL): Configure wallpaper in Windows as "Slideshow" of parent folder of target1 & target2.
# - Linux / GNOME 3: To set wallpaper immediately, uncomment section "Set GNOME 3 wallpaper" at the end. If desired comment out target2.
# 3. Periodically execute this script (e.g. daily), e.g. using cron (Linux) or Task Scheduler (Windows/WSL).
# 4. Optionally adjust overlay position if required.
# Original Source:
# Updates:
# - 2022-01-03: Add documentation, add horizontal 2x tiling for 5120x1440 ultra-widescreen (commented out)
# - 2021-07-08: Make it safer for use, remove all rm statements, remove intermediate files after processing
# - 2014-05-28: Jeremie Miserez <>
# - Changed xmlURL to use the same feed as the official Bing Desktop app
# - Added extraction and stamping of copyright information onto the
# image. Note that this is usually the image description plus the name
# of the photographer/copyright info.
# - NOTE: this script now requires ImageMagick and readlink to be
# installed as well as a font Segoe UI Italic "segoeuii.ttf" to reside in the
# same directory as this script.
# $saveDir is a temporary folder for downloads and intermediate files.
echo "Scratch directory: $saveDir"
# Get this script's path. This method relies on readlink to be installed
SCRIPT=$(readlink -f $0)
echo "Script directory: $SCRIPTPATH"
echo "Checking if font exists at $OVERLAYFONT"
if [ ! -f $OVERLAYFONT ];
echo "Error: font $OVERLAYFONT not found. Exiting."
exit 1
# Create saveDir if it does not already exist
mkdir -p $saveDir
# $bing is needed to form the fully qualified URL for
# the Bing pic of the day
# $xmlURL is needed to get the xml data from which
# the relative URL for the Bing pic of the day is extracted
# The mkt parameter determines which Bing market you would like to
# obtain your images from.
# Valid values are: en-US, zh-CN, ja-JP, en-AU, en-UK, de-DE, en-NZ, en-CA.
# The idx parameter determines where to start from. 0 is the current day,
# 1 the previous day, etc.
# The desired Bing picture resolution to download
# Valid options: "_1024x768" "_1280x720" "_1366x768" "_1920x1200"
# The file extension for the Bing pic
# Extract the relative URL of the Bing pic of the day from
# the XML data retrieved from xmlURL, form the fully qualified
# URL for the pic of the day, and store it in $picURL
# Form the URL for the desired pic resolution
echo "Fetching $xmlURL"
xmlContent=$(curl -s $xmlURL)
echo "$xmlContent"
desiredPicURL=$bing$(echo $xmlContent | grep -oP "<urlBase>(.*)</urlBase>" | cut -d ">" -f 2 | cut -d "<" -f 1)$desiredPicRes$picExt
echo "Desired resolution image URL: $desiredPicURL"
# Form the URL for the default pic resolution
defaultPicURL=$bing$(echo $xmlContent | grep -oP "<url>(.*)</url>" | cut -d ">" -f 2 | cut -d "<" -f 1)
echo "Default resolution image URL: $defaultPicURL"
copyrightInfo=$(echo $xmlContent | grep -o '<copyright>.*</copyright>' | sed 's/\(<copyright>\|<\/copyright>\)//g')
echo "Copyright info: $copyrightInfo"
# $picName now contains the filename of the Bing pic of the day
echo "Checking $desiredPicURL"
# Attempt to download the desired image resolution. If it doesn't
# exist then download the default image resolution
if wget --quiet --spider "$desiredPicURL"
echo "Fetching $desiredPicURL"
# Set picName to the desired picName
# Download the Bing pic of the day at desired resolution
curl --create-dirs -s -o $saveDir$picName $desiredPicURL
# Add copyright information to image (needs ImageMagick and font "segoeuii.ttf" in same directory)
# Write copyright text
echo "Creating copyright stamp layer using $OVERLAYFONT on: $copyrightPicName"
convert -size 1920x50 xc:none -gravity Southwest -fill 'rgba(255,255,255,0.8)' -font $OVERLAYFONT -pointsize 24 -annotate +0+0 "$copyrightInfo" -trim $saveDir$copyrightPicName
# Composite it over the original
echo "Blending into final image: $finalPicName"
composite -blend 50 -gravity Southwest -geometry +20+130 $saveDir$copyrightPicName $saveDir$picName $saveDir$finalPicName
composite -compose pegtop-light -gravity Southwest -geometry +20+130 $saveDir$copyrightPicName $saveDir$finalPicName $saveDir$finalPicName
# tile horizontally for dual widescreen
# convert +append $saveDir$finalPicName $saveDir$finalPicName $saveDir$finalPicName
echo "URL not available, fetching default resolution instead: $defaultPicURL"
# Set picName to the default picName
# Download the Bing pic of the day at default resolution
curl --create-dirs -s -o $saveDir$picName $defaultPicURL
if [ -f $saveDir$finalPicName ];
echo "Updating $target1"
cp $saveDir$finalPicName "$target1"
echo "Updating $target2"
cp $saveDir$finalPicName "$target2"
#store copyright info
#echo $copyrightInfo > "$saveDir/$copyrightName"
if [ -f $saveDir$picName ];
echo "Cleaning up $saveDir$picName"
rm $saveDir$picName
if [ -f $saveDir$copyrightPicName ];
echo "Cleaning up $saveDir$copyrightPicName"
rm $saveDir$copyrightPicName
if [ -f $saveDir$finalPicName ];
echo "Cleaning up $saveDir$finalPicName"
rm $saveDir$finalPicName
# Set GNOME 3 wallpaper
## Valid options are: none,wallpaper,centered,scaled,stretched,zoom,spanned
## Set the GNOME3 wallpaper
# DISPLAY=:0 GSETTINGS_BACKEND=dconf gsettings set org.gnome.desktop.background picture-uri '"file://'$target1'"'
## Set the GNOME 3 wallpaper picture options
# DISPLAY=:0 GSETTINGS_BACKEND=dconf gsettings set org.gnome.desktop.background picture-options $picOpts
echo "Done."
# Exit the script
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment