Last active
December 18, 2015 23:49
-
-
Save itod/5864657 to your computer and use it in GitHub Desktop.
A little Python CoreGraphics scaffolding.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/python | |
| from Quartz.CoreGraphics import * | |
| from Quartz.ImageIO import * | |
| def write_image_to_file(ctx, path): | |
| img = CGBitmapContextCreateImage(ctx) | |
| url = CFURLCreateWithFileSystemPath(None, path, kCFURLPOSIXPathStyle, True) | |
| dest = CGImageDestinationCreateWithURL(url, "public.png", 1, None); | |
| CGImageDestinationAddImage(dest, img, None); | |
| if not CGImageDestinationFinalize(dest): | |
| print("Failed to write image to ", path); | |
| w = 100 | |
| h = 100 | |
| ctx = CGBitmapContextCreate(None, w, h, 8, (4 * w), | |
| CGColorSpaceCreateDeviceRGB(), | |
| kCGImageAlphaPremultipliedFirst) | |
| # fill bg white | |
| CGContextSetRGBFillColor(ctx, 1, 1, 1, 1) | |
| CGContextFillRect(ctx, CGRectMake(0, 0, w, h)) | |
| # set colors & line width | |
| CGContextSetRGBFillColor(ctx, 1, 0, 0, 1) | |
| CGContextSetRGBStrokeColor(ctx, 1, 1, 0, 1) | |
| CGContextSetLineWidth(ctx, 10) | |
| # stroke and fill a rect | |
| r = CGRectMake(w/4, h/4, w/2, h/2) | |
| CGContextAddRect(ctx, r) | |
| CGContextDrawPath(ctx, kCGPathFillStroke) | |
| # write to an image file on disk | |
| write_image_to_file(ctx, "/Users/itod/Desktop/testimage.png") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment