# image_panel.py # Dexter Kozen (dck10) and Walker White (wmw2) # October 26, 2012 """This module provides an additional View for our application. This is the primary View for coordinating with ImageProcessor. Therefore, we separated it as a new class for you to see, rather than putting it in a .kv file.""" from kivy.graphics import Rectangle class ImagePanel(object): """Instance maintains a kivy Image widget on which to display an image. Instance variables: widget (Kivy widget): Widget where image will be displayed image (ImageArray): Reference to image array for display Both variables are set by the constructor and never modified. """ def __init__(self, widget, image): """**Constructor**: Create an ImagePanel in the given widget. :param widget: The widget to display the image in **Precondition**: a Kivy Widget :param image: image to display. **Precondition**: an ImageArray object Changes to the image will only be shown in the ImagePanel when you invoke the display method.""" self.widget = widget self.image = image self.widget.bind(pos=self.move_cb,size=self.move_cb) def move_cb(self,obj,value): """Callback to respond to size/position changes.""" self.display() def display(self,image=None): """Display an image array. :param image: An image to display. **Precondition**: an ImageArray object or None If no image_array is specified, it will use the one stored in its `image` attribute.""" if image is not None: self.image = image texture = self.image.get_texture() # calculate placement of rectangle to draw the texture w,h = self.widget.size # bounding box size a = self.image.cols b = self.image.rows if h*a > w*b: ra = w rb = w*b/a else: rb = h ra = h*a/b pos = (int((w-ra)/2 + self.widget.pos[0]), int((h-rb)/2 + self.widget.pos[1])) size = (int(ra), int(rb)) tc = [0, 1, 1, 1, 1, 0, 0, 0] # display it self.widget.canvas.clear() with self.widget.canvas: Rectangle(texture=texture, tex_coords=tc, pos=pos, size=size)