#! /usr/bin/python # -*- coding: utf-8 -*- # Dual screen wallpaper creator ############################################################################### # # # 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 # # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # # 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 . # # # ############################################################################### # Readme: # This script was created to fullfill my needs, but if you find anything that # could be added, feel free to ask. # It is to be used with Gnome (it uses gconf), but can probably be adapted for # other desktop environments. # Dependency to install on Ubuntu: python-imaging and python-gconf # Exemple cron command (if the script is in the PATH, for exemple $HOME/bin): # 0,15,30,45 * * * * duallpaper.py # TODO: # - Add ability to choose dark, colorfull or light wallpaper depending on # the current time or weather # - Add ability to allow a slight difference in ratio # - Add ability to choose wether or not subdirectories should be scanned # - Store configuration in a separate file? (~/.config/duallpaper.rc) # - Add ability to choos two specific images (as arguments of the command) # - Add ability to configure one set of directories for each screen # - Add ability to automatically detect screens (number, size and positions) # - Add ability to use configurations other than "stretched" for the images import gconf import os import glob import random import Image # Configure screen dimensions # Screen1 is on the left, Screen2 is on the right # Screens are aligned at the top W1 = 1280 H1 = 800 W2 = 1440 H2 = 900 # Disable the upscaling if you don't want a small image to be streched on your HD screen disable_upscaling = 1 # Ask to keep the ratio if you don't want a 4:3 image to be streched on your wide screen # For the moment, even the slightest difference in ratio will be taken in account (1280x768 != 1280x800) keep_ratio = 1 # Set the path of the wallpapers Folders = ["/home/user/pictures/lolcats","/home/user/wallpapers"] def changeBG(): # Get gconf's default client (Gnome) __client__ = gconf.client_get_default () # Get a list of the files from the specified directory files = getFileList(Folders, ['.png', '.jpeg', '.jpg']) # Choose two wallpapers randomly validImage = 0 while (not validImage): im1, validImage = chooseImage(W1,H1,files) validImage = 0 while (not validImage): im2, validImage = chooseImage(W2,H2,files) bg = Image.new("RGB", (W1 + W2,max(H1,H2))) bg.paste(im1, (0,0,W1,H1)) bg.paste(im2, (W1,0,W1+W2,max(H1,H2))) bg.save(os.path.join(os.path.expanduser( "~"), ".background.jpg"), "JPEG", quality=100) # Sets the current wallpaper to the new random picture from specified directory __client__.set_string ("/desktop/gnome/background/picture_options", "wallpaper") __client__.set_string ("/desktop/gnome/background/picture_filename", os.path.join (os.path.expanduser( "~"), ".background.jpg")) return 1 def getFileList (dirPath, file_type): fileList=list() for Folder in dirPath: for dirpath,dirnames,filenames in os.walk(Folder): for file in filenames: if os.path.splitext(file)[1].lower() in file_type: fileList.append(os.path.join(dirpath, file)) return fileList def chooseImage(W,H,fileList): loop = w = h = 1 while (loop or ((w < W or h < H)*(disable_upscaling) or ( 1.0*w/h != 1.0*W/H)*(keep_ratio))) : loop = 0 randomImage = random.choice(fileList) new_im = Image.open(randomImage) w,h = new_im.size try: chosenImage = new_im.resize((W,H), Image.ANTIALIAS) validImage = 1 except IOError: print "Oops, image is interlaced: ", randomImage chosenImage = 0 validImage = 0 return chosenImage, validImage if __name__ == "__main__": changeBG()