def drawWatermarkText(writeContext, line, xOffset, yOffset, angle, scale, opacity): if line: rect = CTLineGetImageBounds(line, writeContext) imageWidth = rect.size.width imageHeight = rect.size.height Quartz.CGContextSaveGState(writeContext) Quartz.CGContextSetAlpha(writeContext, opacity) Quartz.CGContextTranslateCTM(writeContext, xOffset, yOffset) Quartz.CGContextTranslateCTM(writeContext, imageWidth / 2, imageHeight / 2) Quartz.CGContextRotateCTM(writeContext, angle * math.pi / 180) Quartz.CGContextTranslateCTM(writeContext, -imageWidth / 2, -imageHeight / 2) Quartz.CGContextScaleCTM(writeContext, scale, scale) Quartz.CGContextSetTextPosition(writeContext, 0.0, 0.0) CTLineDraw(line, writeContext) Quartz.CGContextRestoreGState(writeContext)
def drawWatermarkText(ctx, line, xOffset, yOffset, angle, scale, opacity): # CGContextRef ctx # CTLineRef line # float xOffset, yOffset, angle ([degree]), scale, opacity ([0.0, 1.0]) if line: rect = CTLineGetImageBounds(line, ctx) imageWidth = rect.size.width imageHeight = rect.size.height CG.CGContextSaveGState(ctx) CG.CGContextSetAlpha(ctx, opacity) CG.CGContextTranslateCTM(ctx, xOffset, yOffset) CG.CGContextScaleCTM(ctx, scale, scale) CG.CGContextTranslateCTM(ctx, imageWidth / 2, imageHeight / 2) CG.CGContextRotateCTM(ctx, angle * math.pi / 180) CG.CGContextTranslateCTM(ctx, -imageWidth / 2, -imageHeight / 2) CG.CGContextSetTextPosition(ctx, 0.0, 0.0) CTLineDraw(line, ctx) CG.CGContextRestoreGState(ctx)
def average_color_from_screenshot(path=None, path2=None, region=None): if region is None: region = CG.CGRectInfinite # Create screenshot as CGImage image = CG.CGDisplayCreateImage(get_widest_display()) height = CG.CGImageGetHeight(image) width = CG.CGImageGetWidth(image) if path: drawImageToFile(image, path) # Create smaller image2 from captured image width2 = 1 height2 = 1 context = CG.CGBitmapContextCreate(None, width2, height2, 8, width2 * 4, CG.CGColorSpaceCreateDeviceRGB(), 1) CG.CGContextScaleCTM(context, float(width2) / width, float(height2) / height) CG.CGContextSetInterpolationQuality(context, CG.kCGInterpolationHigh) CG.CGContextDrawImage(context, NSMakeRect(0, 0, width, height), image) image2 = CG.CGBitmapContextCreateImage(context) if path2: drawImageToFile(image2, path2) # Extract pixel value prov2 = CG.CGImageGetDataProvider(image2) data2 = CG.CGDataProviderCopyData(prov2) c1 = struct.unpack_from("BBBB", data2, offset=0) c2 = NSColor.colorWithCalibratedRed_green_blue_alpha_( c1[0] / 255.0, c1[1] / 255.0, c1[2] / 255.0, c1[3] / 255.0) result = (c2.hueComponent(), c2.saturationComponent(), c2.brightnessComponent()) return result