From: Greg Darke Date: Fri, 13 Mar 2009 11:54:36 +0000 (+1100) Subject: Clean up the convert_image_format functions. X-Git-Url: https://code.delx.au/bg-scripts/commitdiff_plain/a2686cb1eada91d486acc4bbebe57d321fcaa525 Clean up the convert_image_format functions. --- diff --git a/wallchanger.py b/wallchanger.py index 7386293..fa1c5d0 100755 --- a/wallchanger.py +++ b/wallchanger.py @@ -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