Random wiki image wallpaper

Here’s a small idea I wrote about before: change the desktop wallpaper automatically using random images from Wikipedia. Why Wikipedia and not some other source? Because these images are generally more interesting, more diverse and better quality than any other collection I know.

Click this link a couple of times for some examples of random wiki images:

Finally I managed to hack it together and here it is for you to download. I only tried it on Linux but probably it can be ran with small modifications on a Mac as well. If you manage to put together something like this on Windows let me know in the comments. Here are the steps:

1. What we need

  • feh – this is a nice, lightweight image viewer ( https://wiki.archlinux.org/index.php/Feh )
  • convert, mogrify – these are some utilities from the imagemagick set of tools (easily available on most distributions)
  • wget – for fetching files from the internet
  • sed, awk, grep, xargs – you have these already

If you don’t have the first three, on Ubuntu all you need to do is:

sudo apt-get install feh
sudo apt-get install imagemagick
sudo apt-get install wget

2. The main thing

Make a folder where you put the files needed for this wallpaper-changing-business. For me that is ~/random_wallpaper

Create the file run.sh in this newly created folder and copy the code below into it. The code does the following: it fetches the URL of a random image from Wikipedia, it fetches the image itself, makes an attempt to convert it to a decent size (change resolution in the code if necessary), makes an attempt to add the title of the image to the image, sets it as wallpaper, waits for an hour, removes the image and repeats. Additionally, it saves filenames and timestamps in an archive.txt file, so that if you see something interesting, you can look it up later. Feel free to tweak it to your needs, for example, the 60 minutes repeat period in the end could be made 20s or whatever else.

while true; do
	# Remove old file and get new random image
	cat filename.txt | xargs rm
	echo -n "http://upload" > url.txt
	wget -q -O - "$@" http://commons.wikimedia.org/wiki/Special:Random/File | grep -m 1 "href=\"//upload" | sed -e 's/.*href\=\"\/\/upload//g' | sed -e 's/\".*//g' >> url.txt
	url=`cat url.txt`
	wget -O temp_image.tmp "$url"
	echo -n "temp_image." > filename.txt
	cat url.txt | sed -e 's/.*\///g' | sed -e 's/.*\.//g' >> filename.txt
	filename=`cat filename.txt`
	mv temp_image.tmp "$filename"

	# Get filename of image for display
	echo $url | awk -F"/" '{print $NF}' | awk -F. '{print $1}' > urldisplay.txt
	urldisplay=`cat urldisplay.txt`

	# Resize image and add title (filename)
	mogrify -resize 1024x768\> "$filename"  
	convert -pointsize 20 -fill red -draw "text 10,30 "$urldisplay""  "$filename" "$filename"

	# Set as wallpaper
	feh --bg-center "$filename"

	# Leave some traces in the logfile
	echo -n `date` >> archive.txt
	echo -n "    " >> archive.txt
	echo $url >> archive.txt

	# Go to sleep
	sleep 60m

Make this file executable:

chmod +x run.sh

3. Wrapping it up

All we need now is a way of running our script. If you want to try it out just once, or you want to use it as a slideshow on a birthday party, you can simply run it from the terminal:

./run.sh &

If you want to use it all the time, it should be called at each startup. This can be achieved in different ways, depending a bit on the setup used.
I run Fluxbox as a window manager, which is closely related to Blackbox or Openbox.
Here I add towards the end of my startup file the following:

~/random_wallpaper/run.sh &

If you use Gnome, you could add the same line to your ~/.xinitrc file. The Feh documentation says that to make it play nicely with Feh, you should disable Nautilus’ control of the desktop:

gconftool-2 --set /apps/nautilus/preferences/show_desktop --type boolean false

An alternative way of calling run.sh would be to set up a periodic job using cron. In this case the loop (the while- and the done- lines) are not needed.

Let me know in the comments how it worked out, or if you know a simpler way of setting it all up.


#1 Rainer on 11.26.14 at 4:23 pm

Not talking about any tech stuff here: I can only recommend pre-selecting a bunch of images. Or pull them out of a category and their subcategories, [like our featured pictures](https://commons.wikimedia.org/wiki/Category:Featured_pictures). The [last year’s POTY candidates](https://commons.wikimedia.org/wiki/Commons:Picture_of_the_Year/2013/Candidates) are also an excellent choice. The reason is that Wikimedia Commons is not censored and hosts literally all kind of media files.

#2 lkozma on 11.26.14 at 4:40 pm

Thanks Rainer, those categories of photos are gorgeous. Surprisingly, picking absolutely at random also gives nice results most of the time. But it is true that using the script as it is, makes it a bit difficult to use a laptop in public due to the occasional ugly/disturbing picture.

Leave a Comment