Package omero :: Package util :: Module ROIDrawingUtils
[hide private]
[frames] | no frames]

Source Code for Module omero.util.ROIDrawingUtils

  1  #!/usr/bin/env python 
  2  # -*- coding: utf-8 -*- 
  3  # 
  4  # 
  5  #------------------------------------------------------------------------------ 
  6  #  Copyright (C) 2006-2009 University of Dundee. All rights reserved. 
  7  # 
  8  # 
  9  #       This program is free software; you can redistribute it and/or modify 
 10  #  it under the terms of the GNU General Public License as published by 
 11  #  the Free Software Foundation; either version 2 of the License, or 
 12  #  (at your option) any later version. 
 13  #  This program is distributed in the hope that it will be useful, 
 14  #  but WITHOUT ANY WARRANTY; without even the implied warranty of 
 15  #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 16  #  GNU General Public License for more details. 
 17  # 
 18  #  You should have received a copy of the GNU General Public License along 
 19  #  with this program; if not, write to the Free Software Foundation, Inc., 
 20  #  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 
 21  # 
 22  #------------------------------------------------------------------------------ 
 23   
 24  ### 
 25  # 
 26  # ROIDrawingCanvas draws the shapes from the obejct visited.  
 27  # These map to the ROI types in omero.model.*  
 28  # 
 29  # @author  Jean-Marie Burel      
 30  #       <a href="mailto:j.burel@dundee.ac.uk">j.burel@dundee.ac.uk</a> 
 31  # @author       Donald MacDonald &nbsp;&nbsp;&nbsp;&nbsp; 
 32  #       <a href="mailto:donald@lifesci.dundee.ac.uk">donald@lifesci.dundee.ac.uk</a> 
 33  # @version 3.0 
 34  # <small> 
 35  # (<b>Internal version:</b> $Revision: $Date: $) 
 36  # </small> 
 37  # @since 3.0-Beta4 
 38  # 
 39   
 40  """ 
 41   
 42  Example code to draw 10 ellipses randomly on an image. 
 43   
 44  import ROI_utils; 
 45  import ROIDrawingUtils; 
 46  from random import randint; 
 47   
 48  l = []; 
 49  for i in range(0,10): 
 50      e = ROI_utils.EllipseData(ROI_utils.ROICoordinate(), randint(100, 300), randint(100, 300), randint(20, 50), randint(20, 50)) 
 51      l.append(e); 
 52  d = ROIDrawingUtils.DrawingCanvas(); 
 53  d.createImage(400,400) 
 54  v = d.drawElements(l); 
 55  d.image.show()  
 56   
 57   
 58  Example code to draw a polyline on an image an display it in PIL. 
 59   
 60  try: 
 61      from PIL import Image, ImageDraw # see ticket:2597 
 62  except ImportError: 
 63      import Image, ImageDraw # see ticket:2597 
 64   
 65  import ROI_utils 
 66  import ROIDrawingUtils 
 67   
 68  drawingCanvas = ROIDrawingUtils.DrawingCanvas(); 
 69  points = [10,30, 40, 80, 100, 150] 
 70  polygonData = ROI_utils.PolylineData(ROI_utils.ROICoordinate(), points); 
 71  drawingCanvas.createImage(400,400); 
 72  drawingCanvas.drawElements([polygonData]); 
 73  drawingCanvas.image.show()  
 74   
 75   
 76  """ 
 77   
 78   
 79  try: 
 80      from PIL import Image, ImageDraw # see ticket:2597 
 81  except ImportError: 
 82      import Image, ImageDraw # see ticket:2597 
 83   
 84   
 85  ## 
 86  # Drawing canvas allows the creation of shapes on an 
 87  # image using PIL, the class can be supplied with an 
 88  # image and will write on that or can create an image. 
 89  # The object will also visit a list of objects supplied  
 90  # and draw their respective shapes if they accept the 
 91  # DrawingCanvas visior. 
 92  # 
93 -class DrawingCanvas:
94 95 ## 96 # Create the default object. 97 #
98 - def __init__(self):
99 self.width = 0; 100 self.height = 0; 101 self.image = None; 102 self.draw = None;
103 104 ## 105 # Create a new image to draw on with width, height and background colour (0,0,0,0) 106 # @param width See above. 107 # @param height See above.
108 - def createImage(self, width, height):
109 self.image = Image.new('RGBA', (width, height), (0, 0, 0, 0)); 110 self.width = width; 111 self.height = height;
112 113 ## 114 # Set the image to draw on as image which has width, height. 115 # @param image The image to draw on. 116 # @param width See above. 117 # @param height See above.
118 - def setImage(self, image, width, height):
119 self.image = image; 120 self.width = width; 121 self.height = height;
122 123 ## 124 # Visit all the elements in the element list and draw their shapes. 125 # @param elementList See above.
126 - def drawElements(self, elementList):
127 if(self.draw == None): 128 self.draw = ImageDraw.Draw(self.image); 129 for element in elementList: 130 element.acceptVisitor(self); 131 return self.image;
132 133 ## 134 # Get the fill colour from the ShapeSettings object from it's tuple. 135 # @param shapeSetting See above. 136 #
137 - def getFillColour(self, shapeSettings):
138 return shapeSettings[1][0];
139 140 ## 141 # Get the stroke colour from the ShapeSettings object from it's tuple. 142 # @param shapeSetting See above. 143 #
144 - def getStrokeColour(self, shapeSettings):
145 return shapeSettings[0][0];
146 147 ## 148 # Get the stroke width from the ShapeSettings object from it's tuple. 149 # @param shapeSetting See above. 150 #
151 - def getStrokeWidth(self, shapeSettings):
152 return shapeSettings[0][1];
153 154 ## 155 # Draw an ellipse at (cx, cy) with major and minor axis (rx,ry). 156 # @param cx See above. 157 # @param cy See above. 158 # @param rx See above. 159 # @param ry See above. 160 # @param shapeSettings The shapes display properties(colour,etc). 161 # @param affineTransform The affine transform that the shape has to undergo before drawing.
162 - def drawEllipse(self, cx, cy, rx, ry, shapeSettings, affineTransform = None):
163 x = cx-rx; 164 y = cy-ry; 165 w = x+rx*2; 166 h = y+ry*2; 167 fillColour = self.getFillColour(shapeSettings); 168 strokeColour = self.getStrokeColour(shapeSettings); 169 strokeWidth = self.getStrokeWidth(shapeSettings); 170 self.draw.ellipse((x,y,w,h), fill = fillColour, outline = strokeColour);
171 172 ## 173 # Draw a rectangle at (x, y) with width, height (width, height). 174 # @param x See above. 175 # @param y See above. 176 # @param width See above. 177 # @param height See above. 178 # @param shapeSettings The shapes display properties(colour,etc). 179 # @param affineTransform The affine transform that the shape has to undergo before drawing.
180 - def drawRectangle(self, x, y, w, h, shapeSettings, affineTransform = None):
181 fillColour = self.getFillColour(shapeSettings); 182 strokeColour = self.getStrokeColour(shapeSettings); 183 strokeWidth = self.getStrokeWidth(shapeSettings); 184 if(affineTransform==None): 185 self.draw.rectangle((x,y,w,h), fill = fillColour, outline = strokeColour); 186 else: 187 im = Image.new('RGBA', (self.width, self.height), (0, 0, 0, 0)); 188 newDraw = ImageDraw.Draw(im); 189 newDraw.rectangle((x,y,w,h), fill = fillColour, outline = strokeColour); 190 newImage = im.transform((self.width,self.height), Image.AFFINE, affineTransform); 191 self.image.paste(newImage);
192 193 194 ## 195 # Draw an polygon with the points in pointTupleList which are [(x1, y1), (x2, y2)...]. 196 # @param pointTupleList See above. 197 # @param shapeSettings The shapes display properties(colour,etc). 198 # @param affineTransform The affine transform that the shape has to undergo before drawing.
199 - def drawPolygon(self, pointTupleList, shapeSettings, affineTransform = None):
200 fillColour = self.getFillColour(shapeSettings); 201 strokeColour = self.getStrokeColour(shapeSettings); 202 strokeWidth = self.getStrokeWidth(shapeSettings); 203 if(affineTransform==None): 204 self.draw.polygon(pointTupleList, fill = fillColour, outline = strokeColour); 205 else: 206 im = Image.new('RGBA', (self.width, self.height), (0, 0, 0, 0)); 207 newDraw = ImageDraw.Draw(im); 208 self.draw.polygon(pointTupleList, fill = fillColour, outline = strokeColour); 209 newImage = im.transform((self.width,self.height), Image.AFFINE, affineTransform); 210 self.image.paste(newImage);
211 212 ## 213 # Draw a line from (x1, y1) to (x2,y2). 214 # @param x1 See above. 215 # @param y1 See above. 216 # @param x2 See above. 217 # @param y2 See above. 218 # @param shapeSettings The shapes display properties(colour,etc). 219 # @param affineTransform The affine transform that the shape has to undergo before drawing.
220 - def drawLine(self, x1, y1, x2, y2, shapeSettings, affineTransform = None):
221 fillColour = self.getFillColour(shapeSettings); 222 strokeColour = self.getStrokeColour(shapeSettings); 223 strokeWidth = self.getStrokeWidth(shapeSettings); 224 if(affineTransform==None): 225 self.draw.line([(x1, y1), (x2, y2)], fill = strokeColour, width = strokeWidth); 226 else: 227 im = Image.new('RGBA', (self.width, self.height), (0, 0, 0, 0)); 228 newDraw = ImageDraw.Draw(im); 229 self.draw.line([(x1, y1), (x2, y2)], fill = strokeColour, width = strokeWidth); 230 newImage = im.transform((self.width,self.height), Image.AFFINE, affineTransform); 231 self.image.paste(newImage);
232 233 ## 234 # Draw an polyline with the points in pointTupleList which are [(x1, y1), (x2, y2)...]. 235 # @param pointTupleList See above. 236 # @param shapeSettings The shapes display properties(colour,etc). 237 # @param affineTransform The affine transform that the shape has to undergo before drawing.
238 - def drawPolyline(self, pointTupleList, shapeSettings, affineTransform = None):
239 fillColour = self.getFillColour(shapeSettings); 240 strokeColour = self.getStrokeColour(shapeSettings); 241 strokeWidth = self.getStrokeWidth(shapeSettings); 242 if(affineTransform==None): 243 self.draw.line(pointTupleList, fill = fillColour, width = strokeWidth); 244 else: 245 im = Image.new('RGBA', (self.width, self.height), (0, 0, 0, 0)); 246 newDraw = ImageDraw.Draw(im); 247 self.draw.line(pointTupleList, fill = strokeColour, width = strokeColour); 248 newImage = im.transform((self.width,self.height), Image.AFFINE, affineTransform); 249 self.image.paste(newImage);
250 251 ## 252 # Draw a mask at (x, y) with (width, height). 253 # @param x See above. 254 # @param y See above. 255 # @param width See above. 256 # @param height See above. 257 # @param bytes The mask in bytes. 258 # @param shapeSettings The shapes display properties(colour,etc). 259 # @param affineTransform The affine transform that the shape has to undergo before drawing.
260 - def drawMask(self, x, y, width, height, bytes, shapeSettings, affineTransform = None):
261 fillColour = self.getFillColour(shapeSettings); 262 mask = Image.fromstring('1', (width, height), bytes); 263 if(affineTransform==None): 264 self.draw.bitmap(x, y, mask, fill = fillColour); 265 else: 266 im = Image.new('RGBA', (self.width, self.height), (0, 0, 0, 0)); 267 newDraw = ImageDraw.Draw(im); 268 self.draw.bitmap(x, y, mask, fill = fillColour); 269 newImage = im.transform((self.width,self.height), Image.AFFINE, affineTransform); 270 self.image.paste(newImage);
271 272 ## 273 # Draw text at (x, y) with major and minor axis (rx,ryr). 274 # @param x See above. 275 # @param y See above. 276 # @param text The text to draw. 277 # @param shapeSettings The shapes display properties(colour,etc). 278 # @param affineTransform The affine transform that the shape has to undergo before drawing.
279 - def drawText(self, x, y, text, shapeSettings, affineTransform = None):
280 textColour = self.getStrokeColour(shapeSettings); 281 if(affineTransform==None): 282 self.draw.text((x, y), text, fill = textColour); 283 else: 284 im = Image.new('RGBA', (self.width, self.height), (0, 0, 0, 0)); 285 newDraw = ImageDraw.Draw(im); 286 self.draw.text((x, y), text, fill = textColour); 287 newImage = im.transform((self.width,self.height), Image.AFFINE, affineTransform); 288 self.image.paste(newImage);
289