Changeset 148 for nodebox/trunk/src/DrawingPrimitives.py
- Timestamp:
- 09/18/07 18:23:18 (16 months ago)
- Files:
-
- 1 modified
-
nodebox/trunk/src/DrawingPrimitives.py (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
nodebox/trunk/src/DrawingPrimitives.py
r147 r148 782 782 TransformMixin.__init__(self) 783 783 if data is not None: 784 d = NSData.dataWithBytes_length_(data, len(data)) 785 self.image = NSImage.alloc().initWithData_(d) 784 if not isinstance(data, NSData): 785 data = NSData.dataWithBytes_length_(data, len(data)) 786 self.image = NSImage.alloc().initWithData_(data) 786 787 if self.image is None: 787 788 raise NodeBoxError, "can't read image %r" % path … … 1150 1151 for grob in self._grobs: 1151 1152 grob._draw() 1152 1153 def save(self, fname): 1154 basename, ext = os.path.splitext(fname) 1155 if ext != '.tiff': 1156 raise NodeBoxError, "Filename should end in .tiff" 1157 image = NSImage.alloc().initWithSize_((WIDTH, HEIGHT)) 1153 1154 def _get_image(self): 1155 image = NSImage.alloc().initWithSize_((self.width, self.height)) 1158 1156 image.setFlipped_(True) 1159 1157 image.lockFocus() 1160 1158 self.draw() 1161 1159 image.unlockFocus() 1162 fp=open(fname, 'wb') 1163 fp.write(image.TIFFRepresentation()) 1164 fp.close() 1160 return image 1161 image = property(_get_image) 1162 1163 def save(self, fname, format=None): 1164 if format is None: 1165 basename, ext = os.path.splitext(fname) 1166 format = ext[1:].lower() # Skip the dot 1167 1168 imgTypes = {"gif": NSGIFFileType, 1169 "jpg": NSJPEGFileType, 1170 "jpeg": NSJPEGFileType, 1171 "png": NSPNGFileType, 1172 "tiff": NSTIFFFileType} 1173 if format not in imgTypes: 1174 raise NodeBoxError, "Filename should end in .tiff, .gif, .jpg or .png" 1175 1176 data = self.image.TIFFRepresentation() 1177 if format != 'tiff': 1178 imgType = imgTypes[format] 1179 rep = NSBitmapImageRep.imageRepWithData_(data) 1180 data = rep.representationUsingType_properties_(imgType, None) 1181 1182 fname = NSString.stringByExpandingTildeInPath(fname) 1183 data.writeToFile_atomically_(fname, False) 1165 1184 1166 1185 class Context(TransformMixin):
