From 9b31f10905a6b59fab51ceede4b1a5159840db11 Mon Sep 17 00:00:00 2001 From: cramakri <cramakri> Date: Wed, 24 Aug 2011 13:52:28 +0000 Subject: [PATCH] LMS-2472 Initial check-in of python script to generate plate images. SVN: 22624 --- .../python/screening/image-generator.py | 62 +++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 test-data/source/python/screening/image-generator.py diff --git a/test-data/source/python/screening/image-generator.py b/test-data/source/python/screening/image-generator.py new file mode 100644 index 00000000000..0e76ca04079 --- /dev/null +++ b/test-data/source/python/screening/image-generator.py @@ -0,0 +1,62 @@ +#!/usr/bin/python + +# image-generator.py +# +# Create bitmap images in python +# + +from Quartz import * +from Cocoa import * +from LaunchServices import * # for kUTTypePNG +import math + +cs = CGColorSpaceCreateDeviceRGB() + +def write_to_file(ctx, filename): + image = CGBitmapContextCreateImage(ctx) + fileUrl = NSURL.fileURLWithPath_(filename) + dest = CGImageDestinationCreateWithURL(fileUrl, kUTTypePNG, 1, None); + CGImageDestinationAddImage(dest, image, None); + CGImageDestinationFinalize(dest); + + +def createBitmap(text, filename): + pixelsWide = 256 + pixelsHigh = 256 + bitmapBytesPerRow = (pixelsWide * 4); + bitmapByteCount = (bitmapBytesPerRow * pixelsHigh); + + # Create an RGB bitmap context, transparent black background, 256x256 + ctx = CGBitmapContextCreate(None, pixelsWide, pixelsHigh, 8, bitmapBytesPerRow, cs, kCGImageAlphaPremultipliedLast) + + # Draw a yellow square with a red outline in the center + + with CGSavedGState(ctx): + CGContextSetRGBStrokeColor(ctx, 0, 0, 0, 1) # black + CGContextSetRGBFillColor(ctx, 0, 0, 0, 1) # black + CGContextAddRect(ctx, CGRectMake(32.5, 32.5, 191, 191)) + CGContextDrawPath(ctx, kCGPathFillStroke) + + with CGSavedGState(ctx): + CGContextSetRGBStrokeColor(ctx, 0, 0, 0, 1) # black + CGContextSetRGBFillColor(ctx, 1, 1, 1, 1) # white + CGContextSetTextMatrix(ctx, CGAffineTransformIdentity) + CGContextSelectFont(ctx, "Helvetica Neue", 36, kCGEncodingMacRoman) + CGContextSetTextDrawingMode(ctx, kCGTextFillStroke) + CGContextShowTextAtPoint(ctx, 40, 118, text, len(text)) + + # Draw some text at an angle (or not) + # c.saveGState() + # c.setRGBStrokeColor(0,0,0,1) + # c.setRGBFillColor(1,1,1,1) + # c.selectFont("Helvetica", 36, kCGEncodingMacRoman) + # c.setTextPosition(40, 118) + # c.setTextDrawingMode(kCGTextFillStroke) + # c.showText(text, len(text)) + # c.restoreGState() + + # Write the bitmap to disk in PNG format + + write_to_file(ctx, filename) + +createBitmap("hi!", "out.png") \ No newline at end of file -- GitLab