class GeoMarker(GeoPrimitive): #### 'GeoPrimitive' interface ################################################### bounds = (1, 1) # The anchor point of the marker (in relative coordinates) anchor = coordinate_trait((0.5, 0.)) filename = Str _marker = Instance(Image) def _filename_changed(self, new): self._marker = Image(new) ########################################################################### # Protected 'Component' interface. ########################################################################### def _render_primitive(self, gc, view_bounds=None, mode='default'): """ Draw the component. """ x, y = self.position anchor_x, anchor_y = self.anchor w, h = self._marker.width(), self._marker.height() gc.draw_image(self._marker, (x-anchor_x*w, y-anchor_y*h, w, h)) return
class GeoMarker(GeoPrimitive): #### 'GeoPrimitive' interface ################################################### bounds = (1, 1) # The anchor point of the marker (in relative coordinates) anchor = coordinate_trait((0.5, 0.)) filename = Str _marker = Instance(Image) def _filename_changed(self, new): self._marker = Image(new) ########################################################################### # Protected 'Component' interface. ########################################################################### def _render_primitive(self, gc, view_bounds=None, mode='default'): """ Draw the component. """ x, y = self.position anchor_x, anchor_y = self.anchor w, h = self._marker.width(), self._marker.height() gc.draw_image(self._marker, (x - anchor_x * w, y - anchor_y * h, w, h)) return
class ToolbarButton(Button): image = Str() _image = Instance(Image) color = 'black' width = Property(Int, depends_on='label, image') height = Property(Int, depends_on='label, image') # bounds are used for hit testing bounds = Property(List, depends_on='label, image') def __init__(self, *args, **kw): super(ToolbarButton, self).__init__(*args, **kw) image_resource = ImageResource(self.image) self._image = Image(image_resource.absolute_path) @cached_property def _get_width(self): gc = PlotGraphicsContext((100, 100), dpi=72) gc.set_font(self.label_font) (w, h, descent, leading) = gc.get_full_text_extent(self.label) return max(self._image.width(), w) @cached_property def _get_height(self): gc = PlotGraphicsContext((100, 100), dpi=72) gc.set_font(self.label_font) (w, h, descent, leading) = gc.get_full_text_extent(self.label) return self._image.height() + h @cached_property def _get_bounds(self): return [self.width, self.height] def _draw_actual_button(self, gc): x_offset = self.x + (self.width - self._image.width()) / 2 gc.draw_image(self._image, (x_offset, self.y + 2, self._image.width(), self._image.height())) if self.label is not None and len(self.label) > 0: gc.set_font(self.label_font) (w, h, descent, leading) = gc.get_full_text_extent(self.label) if w < self.width: x_offset = self.x + (self.width - w) / 2 else: x_offset = self.x gc.set_text_position(x_offset, self.y - 8) gc.show_text(self.label)
def _draw_mainlayer(self, gc, view_bounds=None, mode="default"): if exists(self.image_file): gc.save_state() img = KivaImage(self.image_file) w, h = img.width(), img.height() self.bounds = [w, h] gc.draw_image(img, (self.x, self.y, w, h)) gc.restore_state()
def __blank_tile_default(self): import pkg_resources import Image as pil import ImageDraw import ImageFont im = pil.new('RGB', (256, 256), (234, 224, 216)) text = 'Image not available' try: font_file = pkg_resources.resource_filename( 'mapping.enable', 'fonts/Verdana.ttf') font = ImageFont.truetype(font_file, 18) except IOError: font = ImageFont.load_default() size = font.getsize(text) pos = (256 - size[0]) // 2, (256 - size[1]) // 2 draw = ImageDraw.Draw(im) draw.text(pos, text, fill=(200, 200, 200), font=font) del draw tile = StringIO() im.save(tile, format='png') return Image(StringIO(tile.getvalue()))
def gc_image_for(name, path=None): "Convert an image file name to a cached Kiva gc containing the image" global _app_path, _enable_path filename = abspath(name) image = _image_cache.get(filename) if image is None: cachename = filename if path is not None: zip_path = abspath(path + '.zip') zip_file = _zip_cache.get(zip_path) if zip_file is None: if is_zipfile(zip_path): zip_file = ZipFile(zip_path, 'r') else: zip_file = False _zip_cache[zip_path] = zip_file if isinstance(zip_file, ZipFile): try: filename = StringIO(zip_file.read(name)) except: pass try: _image_cache[cachename] = image = Image(filename) except: _image_cache[filename] = info = sys.exc_info()[:2] raise info[0], info[1] elif type(image) is TupleType: raise image[0], image[1] return image
def fromfile(cls, filename): """ Alternate constructor to create an ImageData from an image file on disk. 'filename' may be a file path or a file object. """ from kiva.image import Image img = Image(filename) imgdata = cls(data=img.bmp_array, transposed=False) fmt = img.format() if fmt == "rgb24": imgdata.value_depth = 3 elif fmt == "rgba32": imgdata.value_depth = 4 else: raise ValueError("Unknown image format in file %s: %s" % (filename, fmt)) return imgdata
def _draw_mainlayer(self, gc, view_bounds=None, mode="default"): if exists(self.image_file): gc.save_state() # self.image_file.seek(0) img = KivaImage(self.image_file) x = gc.width() * 0.7 y = gc.height() * 0.15 w, h = img.width(), img.height() # Use Image's ability to draw itself onto a gc to paint the window. gc.draw_image(img, (x, y, w, h)) self.position = [x, y] self.bounds = [w, h] gc.restore_state()
def __init__(self, *args, **kw): super(ToolbarButton, self).__init__(*args, **kw) image_resource = ImageResource(self.image) self._image = Image(image_resource.absolute_path)
def _filename_changed(self, new): self._marker = Image(new)
def test_initialization(self): image = Image(self.filename) self.assertEqual(image.width(), 100) self.assertEqual(image.height(), 120) self.assertEqual(image.format(), 'rgb24')
def _tile_cache_changed(self, new): new.process_raw = lambda d: Image(StringIO(d))