Quickies

[categories] [index] [all (531)] [latest]

Python
  1. #!/usr/bin/python
    
    from AppKit import *
    
    WIDTH = 128
    HEIGHT = 64
    
    def draw_something():
        p = NSBezierPath.bezierPath()
    
        p.moveToPoint_((10, 10))
        p.lineToPoint_((50, 60))
        p.lineToPoint_((110, 10))
        p.lineToPoint_((10, 10))
    
        NSColor.redColor().set()
        p.fill()
    
        NSColor.blueColor().set()
        p.stroke()
    
    offscreenRep = NSBitmapImageRep.alloc().initWithBitmapDataPlanes_pixelsWide_pixelsHigh_bitsPerSample_samplesPerPixel_hasAlpha_isPlanar_colorSpaceName_bitmapFormat_bytesPerRow_bitsPerPixel_(None, WIDTH, HEIGHT, 8, 4, True, False, NSCalibratedRGBColorSpace, 0, 4 * WIDTH, 32)
    
    context = NSGraphicsContext.graphicsContextWithBitmapImageRep_(offscreenRep)
    
    NSGraphicsContext.setCurrentContext_(context)
    
    #NSGraphicsContext.saveGraphicsState()
    
    draw_something()
    
    #NSGraphicsContext.restoreGraphicsState()
    
    data = offscreenRep.representationUsingType_properties_(NSPNGFileType, None)
    
    result = data.writeToFile_atomically_("img.png", True)