Topic: show random image on desktop

Hello ..

one friend of mine ask me how he can show some random image on desktop without gadget ( like adesklets ). So I quickly wrote a  python script to do it simply.

#!/usr/bin/python

## Thanks to : ## Lucas Rocha - http://blogs.gnome.org/lucasr/

import gtk
import os
import random
import mimetypes
import gobject

## Images directory
IMG_DIR = '/mnt/data/images/wallpaper/bg'

## Time between change ( in sec )
TIMER = 10

## Background color : HTML Color or None
BG_COLOR = "#202020" 

## Position on desktop
POS_X = 1200
POS_Y = 650

## Image size
WIDTH = 210
HEIGHT = 157

## Show a border :  True or False
SHOW_BORDER = True

class App():
#-----------------------------------------------------------------------
    def __init__(self):
        self.window = gtk.Window() ## gtk.WINDOW_TOPLEVEL)
        self.window.add_events(gtk.gdk.BUTTON_PRESS_MASK)
        self.window.connect("destroy", gtk.main_quit)
        self.window.connect("button_press_event", self.onClick)
        self.window.stick()
        self.window.set_decorated(False)
        self.window.set_type_hint(gtk.gdk.WINDOW_TYPE_HINT_DESKTOP)
        self.window.set_skip_taskbar_hint(True)
        if SHOW_BORDER:
            self.window.set_border_width(1)
        else:
            self.window.set_border_width(0)
        if BG_COLOR:
            self.window.modify_bg(gtk.STATE_NORMAL, gtk.gdk.color_parse(BG_COLOR))
        self.img = gtk.Image()
        dir_items = os.listdir(IMG_DIR)
        self.items = []
        for item in dir_items:
            mimetype = mimetypes.guess_type(item)[0]
            if mimetype and mimetype.split('/')[0] == 'image':
                self.items.append(os.path.join(IMG_DIR, item))
        self.img.set_size_request(WIDTH,HEIGHT)
        self.set_image()
        self.img.show()
        self.window.add(self.img)
        gobject.timeout_add(TIMER*1000, self.set_image)
        self.popupMenu = gtk.Menu()
        menuPopup = gtk.ImageMenuItem (gtk.STOCK_QUIT)
        menuPopup.connect("activate", gtk.main_quit)
        self.popupMenu.add(menuPopup)
        self.popupMenu.show_all()
        self.window.move(POS_X, POS_Y)
        self.window.show_all()

    def set_image(self, widget=None, event=None):
        item = random.randint(0, len(self.items)-1)
        pixbuf = gtk.gdk.pixbuf_new_from_file_at_size(self.items[item],WIDTH,HEIGHT)
        self.img.set_from_pixbuf(pixbuf)
        return True

    def onClick(self, widget, event, data=None):
        if event.button==3: # right click
            self.popupMenu.popup(None, None, None, event.button, event.time)
        elif event.button==1: # left click
            self.set_image()

    def run(self):
        gtk.main()

if __name__ == "__main__":
    my_image = App()
    my_image.run()

Here a screenshot ,  I place image just below conky  : 

SIZE 1440 x 900  -  random_image_on_desktop

http://www.ad-comp.be/public/projets/rnd_img/img/.random_image_on_desktop_m.jpg

Maybe it can be useful for someone ..

Bye

Last edited by ADcomp (2009-04-30 11:16:53)

~$ whoami
ADcomp

Re: show random image on desktop

Nice work, thank you for sharing! smile

Re: show random image on desktop

Woow, fantastic. I've been looking for years for such a simple solution. I hate the desklet bloat.This is great in its simpleness and I'm convinced a lot of people would use it if the y knew it existed. Would be great to include this as a configure option in a future crunchbang.
ADcomp, I've been looking to what kind of things you contributed and I'm really impressed by the combination of simple coding and effectiveness. Hat off!!

GNu/Linux: Nu nog schoner: http://linuxnogschoner.blogspot.com/  Dutch

Re: show random image on desktop

@pablokal :   cool  Thank you ..   you're welcome  wink

~$ whoami
ADcomp

Re: show random image on desktop

David, could you write a little extra code so with pressed 'esc' the script stops.
Also is there a way to make this/any python script start only after typing login and a password?


I use your script to show full screen pictures from my "images" folder, and have to 'pkill python' to stop it.

Last edited by klanger (2010-02-13 19:16:11)

Re: show random image on desktop

If I had the nerve to ask something to improve the code it would be that no background would be shown if the image doesn't fit the size dimensions, but that the relative size dimensions of an image within a certain pixel frame would be printed. Or maybe you can achieve this with a transparent background?

GNu/Linux: Nu nog schoner: http://linuxnogschoner.blogspot.com/  Dutch

Re: show random image on desktop

You could make a great two keystroke (program)launcher with this if you could shuffle through the pictures with the Space key and select a command linked to a particular picture with the Enter key.
Making a table or a grid with more pictures in it (four or nine or twelve) that are selectable with the arrow key and/or time interval would create a dynamic program menu that is very easily selectable without a touch screen. I think this concept is original. I 've never seen this before and I think it would be great to have this.

Uses: fast visual and dynamic program launcher, learning keybindings (if you show the keybinding in the pic) or creating an alternative for using them(for openbox setup newbies), create a kids launcher and last but not least it would be very useful for disabled people.
I have a severely disabled daughter and I'm always looking for angles of using /creating enabling software on which linux is light years away from the sophistication of MS software I'm sorry to say (there's a lot of money made in that field).

Last edited by pablokal (2010-02-14 08:00:32)

GNu/Linux: Nu nog schoner: http://linuxnogschoner.blogspot.com/  Dutch

Re: show random image on desktop

@ klanger :   
Right clic on image => Quit  .. But It's easy to add a little extra code for 'esc'. 
If you need a password , you can check if script is launched with sudo/gksudo .. Or you really want a box with login/passwd ?

@ pablokal:
It's esay to add a transparent background but you need a composite manager ( xcompmgr ) .. Or  It's possible to check aspect/ratio of image and resize/move the "frame".

For "(program)launcher with this if you could shuffle through the pictures" , maybe you can take a look to this : http://crunchbanglinux.org/forums/topic … bookremix/
I can modify it if you want.

~$ whoami
ADcomp

Re: show random image on desktop

Thanks for the tip, and yes I would like to run this script with a passoword so only after confirmation it would show photos located in a folder

Re: show random image on desktop

Hello ,

What's new ?

* Fullscreen mode ( like screensaver )
* add some key 'event' :  Esc -> exit  ,  Space -> pause 
* transparent border ( need composite )
* ask password .. ( not very powerful .. easily crackable )
* ..

Source : http://www.ad-comp.be/public/projets/rn … desklet.py

For password , I use 'rot13' ..  You can create a new password like this :

david@david-desktop:~$ python -c "print 'mypassword'.encode('rot13')"
zlcnffjbeq
~$ whoami
ADcomp

Re: show random image on desktop

Ehhh.... For the password, I would recommend encrypting user input, and using SHA encryption.  Mind me, I know nothing of python, but upon looking at the wikipedia page you provided for ROT13, it looks like its TOO easily crackable, given its known usage.

I know that when I do password encryption in my PHP programming, I use SHA on user input, given there's really only two easy choices for encryption in PHP, MD5 or SHA, and SHA is the best.  I even found in a google search that there is a python module for SHA encryption.

[Acer Aspire One ZG5|Intel Atom N270|1GB RAM|8GB SSD|Intel Integrated Graphics|#! 9.04]
[Custom Desktop|Intel C2D E8400|2GB RAM|80GB & 250GB HDD|NVIDIA GeForce GT220|Win7Pro|#! 9.04]
[last.fm|deviantART|Bloodstar Studios]

Re: show random image on desktop

Thanks for this update and three great extensions of the possible use: full screen, pause and transparent border! Excellent!!
Aren't for full screen mode a zoom or/and scale options maybe important??

GNu/Linux: Nu nog schoner: http://linuxnogschoner.blogspot.com/  Dutch

Re: show random image on desktop

@ kmason : I don't think so.. you have access to python script  (not like php), it's really easy to modify the code .. And yes , 'rot13' is really easily crackable (if you know what is it  wink )

@ pablokal : zoom or/and scale options maybe important? .. for me ? no , but why not .. next update  smile

Last edited by ADcomp (2010-02-16 00:11:12)

~$ whoami
ADcomp

Re: show random image on desktop

Would it be possible to make this ignore all window rules and go above everything else, even tint2 panels, as well as  conky windows that ignore the desktop margin settings from own_window_type being set to override?  It would make a really cool "interactive" screensaver.

[Acer Aspire One ZG5|Intel Atom N270|1GB RAM|8GB SSD|Intel Integrated Graphics|#! 9.04]
[Custom Desktop|Intel C2D E8400|2GB RAM|80GB & 250GB HDD|NVIDIA GeForce GT220|Win7Pro|#! 9.04]
[last.fm|deviantART|Bloodstar Studios]

Re: show random image on desktop

@ kmason : did you try "fullscreen" mode ?

~$ whoami
ADcomp

Re: show random image on desktop

Thanks a lot for this interesting script.
I'd like to ask two questions :
-- 1/ Would it be possible to get a window opened at launching, so as to type the name of the folder we want ? (I think it's possible in batch, with Zenity, but I'm not sure... and I don't know anything about python !).
-- 2/ Is it possible to watch the photos in a definite order, i. e. in the order they are stocked in the folder ?
Sorry for my approximate English.

Re: show random image on desktop

David thanks for the password, but it doesn't work for me - every time I type your password zlcnffjbeq i get an info that it is not correct ;(