FYI, once I updated PyObjC from svn, my example code worked, thanks again Ronald.

<example saveas='image.py'>
from objc import Category
from AppKit import *
from os.path import splitext

_fileRepresentationMapping = {
        '.png': NSPNGFileType,
        '.gif': NSGIFFileType,
        '.jpg': NSJPEGFileType,
        '.jpeg': NSJPEGFileType,
        '.bmp': NSBMPFileType,
        '.tif': NSTIFFFileType,
        '.tiff': NSTIFFFileType,
    }

def _getFileRepresentationType(filepath):
    base, ext = splitext(filepath)
    return _fileRepresentationMapping[ext.lower()]

class NSImage(Category(NSImage)):

    def rect(self):
        return (0,0),self.size()

    @classmethod # for Python2.3 replace with read_ = classmethod(read_)
    def read_(cls, filepath):
        return NSImage.alloc().initWithContentsOfFile_(filepath)

def write_(self, filepath):
self.lockFocus()
image_rep = NSBitmapImageRep.alloc().initWithFocusedViewRect_(self.rect())
self.unlockFocus()
representation = _getFileRepresentationType(filepath)
data = image_rep.representationUsingType_properties_(representation, None)
data.writeToFile_atomically_(filepath, False)

    def fill_(self, color):
        self.lockFocus()
        color.set()
        NSBezierPath.fillRect_(self.rect())
        self.unlockFocus()
</example>

And an example for building images from the command-line

<example>
from AppKit import NSApplication, NSBezierPath, NSColor, NSImage
from Foundation import NSInsetRect, NSMakeRect
import image

app = NSApplication.sharedApplication()

def newImage(width, height):
    image = NSImage.alloc().initWithSize_((width, height))
    image.fill_(NSColor.whiteColor())
    return image

def drawRedCircleOnImage(image):
    image.lockFocus()
    NSColor.redColor().set()
    rect = NSInsetRect(image.rect() 8, 8)
    circle = NSBezierPath.bezierPathWithOvalInRect_(rect)
    circle.setLineWidth_(4)
    circle.stroke()
    image.unlockFocus()
    return image

def drawBlackRectOnImage(image):
    image.lockFocus()
    rect = NSInsetRect(image.rect(), 16, 16)
    NSBezierPath.setDefaultLineWidth_(4)
    NSColor.blackColor().set()
    NSBezierPath.strokeRect_(rect)
    image.unlockFocus()
    return image

def test_read_and_write():
    image1 = newImage(64,64)
    drawBlackRectOnImage(image1)
    image1.write_('square.gif')
    image2 = NSImage.read_('square.gif')
    drawRedCircleOnImage(image2)
    image2.write_('circle.png')

if __name__ == '__main__': test_read_and_write()
</example>

--Dethe

"...coding isn't the poor handmaiden of design or analysis. Coding is
where your fuzzy, comfortable ideas awaken in the harsh dawn of reality.
It is where you learn what your computer can do. If you stop coding,
you stop learning."     Kent Beck, Smalltalk Best Practice Patterns


Attachment: smime.p7s
Description: S/MIME cryptographic signature

_______________________________________________
Pythonmac-SIG maillist  -  Pythonmac-SIG@python.org
http://mail.python.org/mailman/listinfo/pythonmac-sig

Reply via email to