Skip to content

Commit

Permalink
Add a very basic Image component.
Browse files Browse the repository at this point in the history
  • Loading branch information
corranwebster committed Oct 3, 2014
1 parent 267d7f3 commit 72734b8
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 0 deletions.
68 changes: 68 additions & 0 deletions enable/primitives/image.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
""" Defines the Label class.
"""

from __future__ import absolute_import

# Enthought library imports
from traits.api import Array, Bool, Enum, Instance, Property, cached_property

# Local imports
from enable.component import Component
from kiva.image import GraphicsContext


class Image(Component):
""" Component that displays a static image
This is extremely simple right now. By default it will draw the array into
the entire region occupied by the component, stretching or shrinking as
needed. It is up to the user to set the bounds, although the from_file
will give something sensible, and we assume that a constraints-based layout
should have a layour_size_hint of the image size.
"""

#: the image data as an array
data = Array(dtype='uint8')

#: the format of the image data (eg. RGB vs. RGBA)
format = Property(Enum('rgb24', 'rgba32'))

#: the size-hint for constraints-based layout
layout_size_hint = Property(data)

#: the image as an Image GC
_image = Property(Instance(GraphicsContext), depends_on='data')

@classmethod
def from_file(cls, filename):
from scipy.misc import imread
data = imread(filename)
return cls(data=data, bounds=data.shape[:2])

def _draw_mainlayer(self, gc, view_bounds=None, mode="normal"):
""" Draws the image. """
with gc:
gc.draw_image(self._image, (self.x, self.y, self.width, self.height))

@cached_property
def _get_format(self):
if self.data.shape[-1] == 3:
return 'rgb24'
elif self.data.shape[-1] == 4:
return 'rgba32'
else:
raise ValueError('Data array not correct shape')

@cached_property
def _get_layout_size_hint(self):
return self.data.shape[:2]

@cached_property
def _get__image(self):
if not self.data.flags['C_CONTIGUOUS']:
data = self.data.copy()
else:
data = self.data
image_gc = GraphicsContext(data, pix_format=self.format)
return image_gc
35 changes: 35 additions & 0 deletions examples/enable/image_draw.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
"""
This demonstrates the most basic drawing capabilities using Enable. A new
component is created and added to a container.
"""
import os
from enable.example_support import DemoFrame, demo_main
from enable.api import ConstraintsContainer, Window
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'
container = ConstraintsContainer(bounds=[500, 500])
container.add(image)
ratio = float(image.data.shape[1])/image.data.shape[0]
container.layout_constraints = [
image.left == container.contents_left,
image.right == container.contents_right,
image.top == container.contents_top,
image.bottom == container.contents_bottom,
image.layout_width == ratio*image.layout_height,
]
return Window(self, -1, component=container)


if __name__ == "__main__":
# Save demo so that it doesn't get garbage collected when run within
# existing event loop (i.e. from ipython).
demo = demo_main(MyFrame)

0 comments on commit 72734b8

Please sign in to comment.