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

Source Code for Module omero.util.ROIDrawingUtils

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