diff --git a/gooey/gui/image_repository.py b/gooey/gui/image_repository.py index 478e763..c29d035 100644 --- a/gooey/gui/image_repository.py +++ b/gooey/gui/image_repository.py @@ -5,55 +5,49 @@ The module is meant to act as a singleton, hence the globals() abuse. Image credit: kidcomic.net ''' -from functools import partial import os -from gooey.gui.util.freeze import get_resource_path - -_image_details = ( - ('program_icon', 'program_icon.ico'), - ('success_icon', 'success_icon.png'), - ('running_icon', 'running_icon.png'), - ('loading_icon', 'loading_icon.gif'), - ('config_icon', 'config_icon.png'), - ('error_icon', 'error_icon.png') -) - -def init(image_dir): - ''' initalize the images from the default directory path ''' - defaults = {variable_name: os.path.join(image_dir, filename) - for variable_name, filename in _image_details} - globals().update(defaults) - -def patch_images(new_image_dir): - ''' - Loads custom images from the user supplied directory - ''' - pathto = partial(os.path.join, new_image_dir) - - if new_image_dir != 'default': - if not os.path.isdir(new_image_dir): - raise IOError('Unable to find the user supplied directory {}'.format(new_image_dir)) - - new_images = ((varname, pathto(filename)) - for varname, filename in _image_details - if os.path.exists(pathto(filename))) - # push the changes into module scope - globals().update(new_images) - - -default_dir = get_resource_path('images') -init(default_dir) +from functools import partial +from gooey.gui.util.freeze import getResourcePath +from gooey.util.functional import merge +filenames = { + 'programIcon': 'program_icon.ico', + 'successIcon': 'success_icon.png', + 'runningIcon': 'running_icon.png', + 'loadingIcon': 'loading_icon.gif', + 'configIcon': 'config_icon.png', + 'errorIcon': 'error_icon.png' +} +def loadImages(targetDir): + defaultImages = resolvePaths(getResourcePath('images'), filenames) + return {'images': merge(defaultImages, collectOverrides(targetDir, filenames))} +def getImageDirectory(targetDir): + return getResourcePath('images') \ + if targetDir == 'default' \ + else targetDir +def collectOverrides(targetDir, filenames): + if targetDir == '::gooey/default': + return {} + pathto = partial(os.path.join, targetDir) + if not os.path.isdir(targetDir): + raise IOError('Unable to find the user supplied directory {}'.format( + targetDir)) + return {varname: pathto(filename) + for varname, filename in filenames.items() + if os.path.exists(pathto(filename))} +def resolvePaths(dirname, filenames): + return {key: os.path.join(dirname, filename) + for key, filename in filenames.items()} diff --git a/gooey/gui/imageutil.py b/gooey/gui/imageutil.py index a4375e1..c66343e 100644 --- a/gooey/gui/imageutil.py +++ b/gooey/gui/imageutil.py @@ -1,35 +1,28 @@ ''' -Created on Jan 20, 2014 - -@author: Chris +Utilities for loading, resizing and converting between PIL and WX image formats ''' +import six +from PIL import Image import wx + from gooey.gui.three_to_four import imageFromBitmap, bitmapFromImage +def loadImage(img_path): + return Image.open(img_path) + + +def resizeImage(im, targetHeight): + im.thumbnail((six.MAXSIZE, targetHeight)) + return im -def _load_image(image_path): - try: - return wx.Bitmap(image_path) - except: - raise IOError('Invalid Image path') +def wrapBitmap(im, parent): + bitmapData = wx.Bitmap.FromBufferRGBA(im.size[0], im.size[1], im.convert('RGBA').tobytes()) + return wx.StaticBitmap(parent, bitmap=bitmapData) -def resize_bitmap(parent, _bitmap, target_height): - ''' - Resizes a bitmap to a height of 89 pixels (the - size of the top panel), while keeping aspect ratio - in tact - ''' - image = imageFromBitmap(_bitmap) - _width, _height = image.GetSize() - if _height < target_height: - return wx.StaticBitmap(parent, -1, bitmapFromImage(image)) - ratio = float(_width) / _height - image = image.Scale(target_height * ratio, target_height, wx.IMAGE_QUALITY_HIGH) - return wx.StaticBitmap(parent, -1, bitmapFromImage(image)) if __name__ == '__main__':