Skip to content

Commit

Permalink
Beef up class constructors. Ensure data value is populated.
Browse files Browse the repository at this point in the history
  • Loading branch information
corranwebster committed Jan 6, 2015
1 parent 045d7f1 commit 2638205
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
11 changes: 8 additions & 3 deletions enable/primitives/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class Image(Component):
"""

#: the image data as an array
data = Array(dtype='uint8')
data = Array(shape=(None, None, (3,4)), dtype='uint8')

#: the format of the image data (eg. RGB vs. RGBA)
format = Property(Enum('rgb24', 'rgba32'))
Expand All @@ -35,11 +35,16 @@ class Image(Component):
_image = Property(Instance(GraphicsContext), depends_on='data')

@classmethod
def from_file(cls, filename):
def from_file(cls, filename, **traits):
from PIL import Image
from numpy import asarray
data = asarray(Image.open(filename))
return cls(data=data, bounds=data.shape[:2])
return cls(data=data, **traits)

def __init__(self, data, **traits):
# the default bounds are the size of the image
traits.setdefault('bounds', data.shape[:2])
super(Image, self).__init__(data=data, **traits)

def _draw_mainlayer(self, gc, view_bounds=None, mode="normal"):
""" Draws the image. """
Expand Down
13 changes: 7 additions & 6 deletions examples/enable/image_draw.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
"""
This demonstrates the most basic drawing capabilities using Enable. A new
component is created and added to a container.
This demonstrates the use of the simple Image component.
"""
import os
from enable.example_support import DemoFrame, demo_main

from enable.api import ConstraintsContainer, Window
from enable.example_support import DemoFrame, demo_main
from enable.primitives.image import Image

THIS_DIR = os.path.split(__file__)[0]


class MyFrame(DemoFrame):

def _create_window(self):
path = os.path.join(THIS_DIR, 'deepfield.jpg')
image = Image.from_file(path)
image.resist_width = 'weak'
image.resist_height = 'weak'
image = Image.from_file(path, resist_width='weak',
resist_height='weak')

container = ConstraintsContainer(bounds=[500, 500])
container.add(image)
ratio = float(image.data.shape[1])/image.data.shape[0]
Expand Down

0 comments on commit 2638205

Please sign in to comment.