Skip to content

Instantly share code, notes, and snippets.

@vighneshbirodkar
Last active December 18, 2015 19:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vighneshbirodkar/5834197 to your computer and use it in GitHub Desktop.
Save vighneshbirodkar/5834197 to your computer and use it in GitHub Desktop.
RenderContext and LayerStack Base classes.
from abc import ABCMeta,abstractmethod
class Display:
__metaclass__ = ABCMeta
#TODO add text for adjusting image size to fit
"""
**SUMMARY**
The Base class for all Displays in SimpleCV. Inheriting classes
All coordinates specified or returned are a (x,y) tuple . The top left is
(0,0) with Left -> Right and Up -> Down being positive
* FULLSCREEN - Opens up a fullscreen display
* DEFAULT - the default display which tries to fit the image in the best
possible way.
**NOTES**
While inheriting , ensure that the original doc string is prepended to the
inherited methods docstring wherever necessary
**EXAMPLE**
>>> display = MumboJumboDisplay()
>>> image = Image('lenna')
>>> image.save(display)
"""
#good ol flags, I think only these would be valid for all platforms
DEFAULT = 0
FULLSCREEN = 1
#Display size doesn't change, come what may !
FIXED = 2
#what to do with a bigger image when display size is fixed
RESIZE = 0
CROP = 1
#The last display initialized, to be used for img.show()
screen = None
#A short string that should indicate the type of dislay
@abstractmethod
def name():
pass
@abstractmethod
def __init__(self,size = (640,480),type_ = DEFAULT,title = "SimpleCV",fit = RESIZE):
"""
**SUMMARY**
Opens up a display in a window.
**PARAMETERS**
* *size* - the size of the diplay in pixels.
* *type_* - Control how the diplay behaves, either FULSCREEN,FIXED or
DEFAULT ( by default ).
* *title* - the title bar on the display, if there exists onw.
* *fit* - How to display the image if the type_ is FIXED. Either CROP or
RESIZE
**EXAMPLE**
>>> disp = Display()
>>> img = Image('simplecv')
>>> img.save(disp)
"""
screen = self
self.size = size
self.type_ = type_
self.fit = fit
self.imgSize = None
self.xScale = 1.0
self.yScale = 1.0
self.image = None
def __repr__(self):
return "<SimpleCV %s resolution:(%s), Image Resolution: (%d, %d) at memory location: (%s)>" % (self.name(),self.size, self.imgSize[0], self.imgSize[1], hex(id(self)))
@abstractproperty
def mousePosition(self):
"""
Reutrns the mouse pointer potion as a tuple of (x,y), with respect to
the image coordinates
"""
@abstractproperty
def mousePositionRaw(self):
"""
Reutrns the mouse pointer potion as a tuple of (x,y), with respect to
the display coordinates
"""
@abstractproperty
def mousePositionRaw(self):
"""
Reutrns the mouse pointer potion as a tuple of (x,y), with respect to
the display coordinates
"""
@abstractmethod
def leftDown(self):
"""
**SUMMARY**
Reutrns the position where the left mouse button last went down,None
if it didn't since the last time this fucntion was called
**RETURNS**
An (x,y) mouse postion tuple where the left mouse button went down.
"""
@abstractmethod
def leftUp(self):
"""
**SUMMARY**
Reutrns the position where the left mouse button last went up,None
if it didn't since the last time this fucntion was called
**RETURNS**
An (x,y) mouse postion tuple where the left mouse button went up.
"""
@abstractmethod
def rightDown(self):
"""
**SUMMARY**
Reutrns the position where the right mouse button last went down,None
if it didn't since the last time this fucntion was called
**RETURNS**
An (x,y) mouse postion tuple where the right mouse button went down.
"""
@abstractmethod
def rightUp(self):
"""
**SUMMARY**
Reutrns the position where the right mouse button last went up,None
if it didn't since the last time this fucntion was called
**RETURNS**
An (x,y) mouse postion tuple where the right mouse button went up.
"""
@abstractmethod
def middleDown(self):
"""
**SUMMARY**
Reutrns the position where the middle mouse button last went down,None
if it didn't since the last time this fucntion was called
**RETURNS**
An (x,y) mouse postion tuple where the middle mouse button went down.
"""
@abstractmethod
def middleUp(self):
"""
**SUMMARY**
Reutrns the position where the middle mouse button last went up,None
if it didn't since the last time this fucntion was called
**RETURNS**
An (x,y) mouse postion tuple where the middle mouse button went up.
"""
@abstractmethod
def setFitMethod(self,method = RESIZE):
"""
**SUMMARY**
Changes how the display accomodates larger images, if it's size is kept
fixed.
**PARAMETERS**
* *method* - the method used to accomodate images.
Options are as follows:
* CROP - Crop the image to fit.
* RESIZE - Resize the image to fit.
**Note**
Will only take effect is display type is FIXED
**RETURNS**
Nothing.
"""
@abstractmethod
def showImage(self, image):
"""
**SUMMARY**
Shows the Image given on the Display
**PARAMETERS**
* *image* - the SimpleCV image to save to the display.
**RETURNS**
Nothing.
**EXAMPLE**
>>> img = Image("lenna")
>>> disp = Display((512,512))
>>> disp.showImage(img)
"""
self.image = img
@abstractmethod
def close(self):
"""
**SUMMARY**
Closes the display window
**RETURNS**
Nothing.
**EXAMPLE**
>>> img = Image("lenna")
>>> disp = Display((512,512))
>>> disp.showImage(img)
>>> disp.close()
"""

Use Cases

import DisplayXYZ
  • This will set DisplayXYZ, DrawingLayerXYZ as the default Display and DrawingLayer classes for all future calls.
img.show()

Can be mapped to

img.save(display)

If a display doesn't exists, a new one will be created. The display always containt a pointer to the current Image being shown

1.Bare Minimum

img = Image('lenna')
img.show()
  • Image Created
  • A DisplayXYZ is created, and the image is shown by calling display.showImage()

2.Drawing things

img = Image('lenna')
img.drawCircle((10,10),5)
img.show()
  • Image Created
  • A DrawingLayerXYZ is created and inserted into the image's drawing layer stack if one doesnt exist
  • drawingLayer.drawCircle((10,10),5) is called, which also stored a vector representaion of the figure in the Layer
  • A DisplayXYZ is created, and the image is shown by calling display.showImage(). DisplayXYZ knows how to draw things in a DrawingLayerXYZ

3.Feature Set

img = Image('lenna')
fs = img.findLines()
img.draw(fs,width=2)
img.show()
  • Image Created
  • Feature Set formed
  • DrawingLayerXYZ is created is none exists and pushed into stack.
  • fs is drawn on by drawingLayer.draw*, which also stored a vector representaions of the figure in the Layer
  • A DisplayXYZ is created, and the image is shown by calling display.showImage().

4.Applying Layers

img = Image('lenna')
img.drawCircle((10,10),5)
drawing = img.applyLayers()
drawing.save('new.jpg')
  • Image created
  • A DrawingLayerXYZ is created and inserted into the image's drawing layer stack if one doesnt exist
  • drawingLayer.drawCircle((10,10),5) is called, which also stored a vector representaion of the figure in the Layer
  • applyLayers iterated through drawing layers forming a bitmap specific to XYZLib
  • SimpleCV image object is retuned (most probably by calling toString() internally)
  • Image saved

5.Events

d = DisplayXYZ((640,480))
#user clicks
d.leftMouse()
  • Display Created
  • LibXYZ cateched the event and assigns the position (x,y) to a local variable
  • leftMouse() returns (x,y)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment