]> code.delx.au - bg-scripts/blobdiff - wallchanger.py
Various hacks
[bg-scripts] / wallchanger.py
index 7386293abb83d9d502a652f22f8ff68fd662abf7..74be97ba43deb3296c3180a887c690e32aedf45f 100755 (executable)
@@ -6,6 +6,10 @@
 
 import commands, sys, os, os.path, time
 import logging
+try:
+       import PIL, PIL.Image
+except ImportError:
+       PIL = None
 
 __all__ = ("init", "set_image")
 
@@ -82,6 +86,27 @@ class BaseChanger(object):
        def set_image(self, filename):
                raise NotImplementedError()
 
+       def convert_image_format(self, filename, format='BMP', allowAlpha=False, extension='.bmp'):
+               """Convert the image to another format, and store it in a local place"""
+               if not os.path.exists(filename):
+                       logger.warn('The input file "%s" does not exist, so it will not be converted', filename)
+                       return filename, False
+               if PIL is None:
+                       logger.warn('PIL could not be found, not converting image format')
+                       return filename, False
+
+               self.remove_old_image_cache()
+               output_name = os.path.join(self._ConvertedWallpaperLocation, '%s%s' % (time.time(), extension))
+               img = PIL.Image.open(filename)
+
+               # Remove the alpha channel if the user doens't want it
+               if not allowAlpha and img.mode == 'RGBA':
+                       img = img.convert('RGB')
+               img.save(output_name, format)
+
+               return output_name, True
+
+
 class WMakerChanger(BaseChanger):
        name = "WindowMaker"
        _ConvertedWallpaperLocation = '/tmp/wallpapers_wmaker/'
@@ -145,10 +170,7 @@ class OSXChanger(BaseChanger):
                self.remove_old_image_cache()
                output_name = os.path.join(self._ConvertedWallpaperLocation, '%s.png' % time.time())
                try:
-                       import PIL, PIL.Image
-                       img = PIL.Image.open(file)
-                       img.save(output_name, "PNG")
-                       return output_name, True
+                       return super(OSXChanger, self).convert_image_format(file, format='PNG', extension='.png')
                except ImportError:
                        logging.debug('Could not load PIL, going to try just copying the image')
                        import shutil
@@ -201,18 +223,6 @@ class WIN32Changer(BaseChanger):
                        for dirname in dirnames:
                                os.unlink(os.path.join(fullpath, dirname))
 
-       def convert_image_format(self, file):
-               """Convert the image to a bmp, and store it in a local place"""
-               self.remove_old_image_cache()
-               output_name = os.path.join(self._ConvertedWallpaperLocation, '%s.bmp' % time.time())
-               import PIL, PIL.Image
-               img = PIL.Image.open(file)
-               if img.mode == 'RGBA':
-                       img = img.convert('RGB')
-               img.save(output_name, 'BMP')
-
-               return output_name, True
-
        def set_image(self, filename):
                import ctypes
                user32 = ctypes.windll.user32