]> code.delx.au - bg-scripts/commitdiff
Clean up the convert_image_format functions.
authorGreg Darke <greg@tsukasa.net.au>
Fri, 13 Mar 2009 11:54:36 +0000 (22:54 +1100)
committerGreg Darke <greg@tsukasa.net.au>
Fri, 13 Mar 2009 11:54:36 +0000 (22:54 +1100)
wallchanger.py

index 7386293abb83d9d502a652f22f8ff68fd662abf7..fa1c5d08b2827a6a1ed135d38787280bddb486ee 100755 (executable)
@@ -82,6 +82,22 @@ 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"""
+               import PIL, PIL.Image
+
+               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 +161,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 +214,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