Browse Source

remove use of gloabls in image loader; use Pillow for smoother resizing

pull/240/head
chriskiehl 7 years ago
parent
commit
8ce8d675c2
2 changed files with 45 additions and 58 deletions
  1. 68
      gooey/gui/image_repository.py
  2. 35
      gooey/gui/imageutil.py

68
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 Image credit: kidcomic.net
''' '''
from functools import partial
import os 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()}

35
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 import wx
from gooey.gui.three_to_four import imageFromBitmap, bitmapFromImage 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__': if __name__ == '__main__':

Loading…
Cancel
Save