Skip to content

Instantly share code, notes, and snippets.

@jmiserez
Last active January 19, 2022 13:21
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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
#!/bin/bash
# exit on error
set -e
# Description:
# - Linux-compatible alternative to the official Bing Wallpaper client (https://www.microsoft.com/en-us/bing/bing-wallpaper).
# - 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:
# http://ubuntuforums.org/showthread.php?t=2074098
# http://stackoverflow.com/questions/10639914/is-there-a-way-to-get-bings-photo-of-the-day/13057735#13057735
#
# 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 <jeremie@miserez.org>
# - 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.
saveDir='/mnt/c/Users/jms/bingdesktop/tmp/'
echo "Scratch directory: $saveDir"
target1='/mnt/c/Users/jms/bingdailywallpaper/slideshow-bingwallpaper-1.jpg'
target2='/mnt/c/Users/jms/bingdailywallpaper/slideshow-bingwallpaper-2.jpg'
# Get this script's path. This method relies on readlink to be installed
SCRIPT=$(readlink -f $0)
SCRIPTPATH=`dirname $SCRIPT`
echo "Script directory: $SCRIPTPATH"
OVERLAYFONT=$SCRIPTPATH/segoeuii.ttf
echo "Checking if font exists at $OVERLAYFONT"
if [ ! -f $OVERLAYFONT ];
then
echo "Error: font $OVERLAYFONT not found. Exiting."
exit 1
fi
# 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
bing="www.bing.com"
# $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.
xmlURL="http://www.bing.com/HPImageArchive.aspx?format=xml&idx=0&n=1"
# The desired Bing picture resolution to download
# Valid options: "_1024x768" "_1280x720" "_1366x768" "_1920x1200"
desiredPicRes="_1920x1200"
# The file extension for the Bing pic
picExt=".jpg"
# 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
finalPicName=$picName
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"
then
echo "Fetching $desiredPicURL"
# Set picName to the desired picName
picName=${desiredPicURL##*/}
# 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
copyrightPicName=$picName'_copyright.png'
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
finalPicName=$picName'_annotated.jpg'
# 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
else
echo "URL not available, fetching default resolution instead: $defaultPicURL"
# Set picName to the default picName
picName=${defaultPicURL##*/}
# Download the Bing pic of the day at default resolution
curl --create-dirs -s -o $saveDir$picName $defaultPicURL
fi
if [ -f $saveDir$finalPicName ];
then
echo "Updating $target1"
cp $saveDir$finalPicName "$target1"
echo "Updating $target2"
cp $saveDir$finalPicName "$target2"
#store copyright info
#copyrightName=$picName'_copyright.txt'
#echo $copyrightInfo > "$saveDir/$copyrightName"
fi
if [ -f $saveDir$picName ];
then
echo "Cleaning up $saveDir$picName"
rm $saveDir$picName
fi
if [ -f $saveDir$copyrightPicName ];
then
echo "Cleaning up $saveDir$copyrightPicName"
rm $saveDir$copyrightPicName
fi
if [ -f $saveDir$finalPicName ];
then
echo "Cleaning up $saveDir$finalPicName"
rm $saveDir$finalPicName
fi
# Set GNOME 3 wallpaper
## Valid options are: none,wallpaper,centered,scaled,stretched,zoom,spanned
#picOpts="zoom"
## 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
exit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment