Feeding a screensaver from the digikam database

After several months of living without any screensaver, I decided it just would be nice to once again have something nice to watch and show while idling. I really like the photo slideshow that ships with windows vista, but as my laptop runs linux only that was not possible. The next best thing i found was glslideshow from the xscreensaver compilation. Like most slideshow programs, glslideshow will look into a given directory, and show files out of it in random order. However, there are downsides to that simple approach: My photo folder contains some private pictures, and also some pictures that are just ugly. So i decided to connect it to my digikam database, which was surprisingly simple.

By default, xscreensaver takes the directory out of its configfile ~/.xscreensaver, and passes it to a helper script xscreensaver-getimage-file, which then prints the complete path to a random file in that directory. So all one needs to do to add another source for pictures is to replace that file, there is already xscreensaver-getimage-flickr
, which pulls photos off flickr. So I wrote a similar script in python to print a random file from my digikam database. Maybe there are more?


The script is written in python, so that has to be installed (which is usually the case), it also requires sqlite and python bindings for it (apt-get install python-pysqlite2 in debian and ubuntu). If those are installed, backup the original xscreensaver-getimage-file, and copy the script there. Check that it is executable.
the overall process would look like(assuming xscreensaver-glslideshow already works)

sudo apt-get install python-pysqlite2
cd ~
wget http://andreas.goelzer.de/download/xscreensaver-getimage-digikam.py
chmod +x xscreensaver-getimage-digikam.py
cd /usr/bin
sudo mv xscreensaver-getimage-file xscreensaver-getimage-file-original 
sudo mv ~/xscreensaver-getimage-digikam.py /usr/bin/
ln -s xscreensaver-getimage-digikam.py xscreensaver-getimage-file

If you had previously pointed xscreensaver to your digikam photo folder, that should work now. If not, edit ~/.xscreensaver and add a line pointing to that directory, in my case:

imageDirectory: /home/goelzera/Documents/Bilder


Right now this is all a bit hacky. at the beginning of the file, there is a line

bad_tags = "('private','me','Nophoto','Notmeanttobenice')"

If called, the script will take a random picture out of the database, then check if it has one of those tags. If it is tagged with one of those, it will be dismissed, similarly, if it has an extension indicating xscreensaver won’t be able to display it. So, set those line to the tags you do not want in your slideshow. For everything else you need to change the sql queries or the source code.


Set your screensaver to glslideshow and wait for it to start

# -*- coding: utf-8 -*-
Script to choose random pictures from the digikam database
Copyright (C) 2008  Andreas Goelzer

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.

This program takes one random picture out of a sqlite-database
made by digikam.
This is useful for screensavers, xscreensaver-glslideshow in particular.

#import sys;
#sys.stderr = ef

good_extensions = ('jpg', 'jpeg', 'pjpeg', 'pjpg', 'png', 'gif', 'tif', 'tiff', 'xbm', 'xpm')
bad_tags = "('private','me','Nophoto','Notmeanttobenice')"
#good_tags = "('public')"
good_tags = ""
attempts = 200

import sqlite3;
from sys import argv, exit;
from os.path import splitext;

digikamversion = "4";

if(len(argv) > 1):
	dirname = argv[len(argv) - 1]
	dirname = "/home/goelzera/Documents/Pictures"
con = sqlite3.connect(dirname + "/digikam" + digikamversion + ".db")
if(digikamversion == "4"):
	url = "relativePath"
	dirid = "album"
	url = "url"
	dirid = "dirid"
#from time import time
#f.write(str(time()) + '\t' + str(argv) + '\t')

#get tagids for bad tags
def getTagIdsFromNames(names):
	tag_ids = ' ';
	for row in con.execute("SELECT id FROM Tags WHERE name IN "+ names):
		tag_ids += str(row[0]) + ','
	tag_ids = '(' + tag_ids[:-1] + ')'
	return tag_ids

if(bad_tags != ""): bad_tag_ids = getTagIdsFromNames(bad_tags)
if(good_tags != ""): good_tag_ids = getTagIdsFromNames(good_tags)

file = 'none';
#search for images, take a random one out of the db and see if it fulfills the criteria
for attempt in range(attempts):
	#get a random picture, i guess this is the wrong way, performs horrible
	#truely random, but painfully slow
	#row = con.execute("SELECT id, name, $dirid FROM Images ORDER BY RANDOM() LIMIT 0,1").fetchone()
	#fast, but not as random
	row = con.execute("SELECT id, name, "+ dirid+" FROM Images WHERE id >= (abs(RANDOM()) % (SELECT max(id) FROM Images)) LIMIT 0,1").fetchone()
	#f.write(str(row[0]) + ',')
	#check for bad tag
	if(bad_tags != ""):
		if(con.execute("SELECT 1 FROM ImageTags WHERE imageid = " + str(row[0]) + " AND tagid IN "+ bad_tag_ids).fetchone()): continue

	#check for good tag
	if(good_tags != ""):
		if(not con.execute("SELECT 1 FROM ImageTags WHERE imageid = " + str(row[0]) + " AND tagid IN "+ good_tag_ids).fetchone()): continue

	#check for invalid extension
	ext = splitext(row[1])
	ext = ext[1][1:].lower()
	if(not ext in good_extensions): continue
	#not rejected, get directory
	drow = con.execute("SELECT "+url+" FROM Albums WHERE id = " + str(row[2])).fetchone()
	file = dirname + drow[0] + '/' + row[1]

print file.encode('utf-8')

#f.write(file.encode('latin-1') + '\n')

filetype xscreensaver-getimage-digikam.py (3.29 kiB, 2009-08-06)

This entry was posted in Computers, Photography and tagged , , , . Bookmark the permalink.

6 Responses to Feeding a screensaver from the digikam database

  1. Nateloaf says:

    This is EXACTLY what I am looking for… I made the required modifications to the script, but no matter what, GLSlideshow just keeps showing some sort of test pattern logo thing (the default image). I have created the .xscreensaver file with the imageDirectory setting, but it appears to be ignored.

    Any suggestions are most welcome…

  2. Andreas Goelzer says:

    You can try starting the program manually:

    xscreensaver-getimage-file ~/Documents/Bilder

    it should return a filename of an image, like

    09:35:00 goelzera@prometheus:~$ xscreensaver-getimage-file ~/Documents/Bilder
    /home/goelzera/Documents/Bilder/Merklingen/Vogelhaus 3/img_3816.jpg
    09:35:00 goelzera@prometheus:~$

    If it doesn’t, it will probably give you some python error(module not found, file not fould, failed to open database or so), if so, post that. If not i guess you’d better hardcode the directory into the file.

  3. Michael says:

    Thanks! it worked as expected.

    I had to install the xscreensaver package then run xscreensaver-demo so i could configure glslideshow. i wanted to turn off the panning and google didn’t tell me what else should be in the .xscreensaver file. It seems that gnome-screensaver includes/depends on glslideshow but not the means to configure it.

    Any idea how to display the file path? in xscreensaver-demo i could only tell it to show the filename, and with names like img0001.jpg that’s not very helpful to me. all the info is in the directory structure.

    I tried passing the path to osd_cat but the filename displayed didn’t seem to match the picture. My best guess is it prefetches the names (and possibly the files too).

  4. Andi says:

    Hi Andreas,

    great work! Exactly what I am looking for.
    But is there a way to include something like “good tags”? So I would be able to add pics to the screensaver by just applying the to them in digikam?
    That would be awsome!

  5. Andreas Goelzer says:

    I added a good_tags option, albeit very crudely. If someone knows of a clean way (and a fast one) to do the tag-filtering with joins, please tell me. Also, it now works with digikam for kde4, which uses a different database structure(i hope it still works for digikam for kde3, if the version variable is set correctly).

  6. You should be able to place the new version in /usr/local/bin – this will work as long as /usr/local/bin is in your path before /usr/bin (check echo $PATH) and the path to xscreensaver-getimage-digikam is not hardcoded anywhere, which it really shouldn’t be. This way you won’t have to rename or move any existing programs.

    That said, a simple modification to this would be to have it read the good/bad tags and database location out of a .config file in the user’s home directory.

    Great work!

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>