Show
Ignore:
Timestamp:
09/18/07 18:23:18 (16 months ago)
Author:
fdb
Message:

- QuickTime? export
- Image formats

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • nodebox/trunk/src/DrawingPrimitives.py

    r147 r148  
    782782        TransformMixin.__init__(self) 
    783783        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) 
    786787            if self.image is None: 
    787788                raise NodeBoxError, "can't read image %r" % path 
     
    11501151        for grob in self._grobs: 
    11511152            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)) 
    11581156        image.setFlipped_(True) 
    11591157        image.lockFocus() 
    11601158        self.draw() 
    11611159        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) 
    11651184 
    11661185class Context(TransformMixin):