Skip to content
Snippets Groups Projects
Commit 7f1898a0 authored by cramakri's avatar cramakri
Browse files

LMS-2472 refactored image generation script

SVN: 22629
parent 8ab2528a
No related branches found
No related tags found
No related merge requests found
from Quartz import *
from Cocoa import *
from LaunchServices import * # for kUTTypePNG
class PlateWellCanvas:
"""
A canvas with utility methods for drawing images for plate wells.
The utility methods support drawing source images and overlays and
allow the resulting images to be written to files.
The canvas uses an underlying CoreGraphics bitmap context with with
device RGB color space.
"""
def __init__(self, width = 512, height = 512):
"""Constructor that takes the size of the canvas."""
self.width = width
self.height = height
self.ctx = self._create_rgba_bitmap_context()
def draw_inset_rect(self, r, g, b, start, isUnfilled = 0):
ctx = self.ctx
with CGSavedGState(ctx):
CGContextSetRGBStrokeColor(ctx, r, g, b, 1)
if isUnfilled:
CGContextSetRGBFillColor(ctx, 0, 0, 0, 0) # transparent
else:
CGContextSetRGBFillColor(ctx, r, g, b, 1)
CGContextSetLineWidth(ctx, 40)
CGContextAddRect(ctx, CGRectMake(start, start, self.width - 2 * start, self.height - 2 * start))
CGContextDrawPath(ctx, kCGPathFillStroke)
def draw_text(self, x, y, text):
ctx = self.ctx
with CGSavedGState(ctx):
CGContextSetRGBStrokeColor(ctx, 0, 0, 0, 1) # black
CGContextSetRGBFillColor(ctx, 1, 1, 1, 1) # white
CGContextSetTextMatrix(ctx, CGAffineTransformIdentity)
CGContextSelectFont(ctx, "Helvetica Neue", 70, kCGEncodingMacRoman)
CGContextSetTextDrawingMode(ctx, kCGTextFillStroke)
CGContextShowTextAtPoint(ctx, x, y, text, len(text))
def write_png_file(self, filename):
"""Write the graphics context to a PNG file"""
ctx = self.ctx
image = CGBitmapContextCreateImage(ctx)
fileUrl = NSURL.fileURLWithPath_(filename)
dest = CGImageDestinationCreateWithURL(fileUrl, kUTTypePNG, 1, None)
CGImageDestinationAddImage(dest, image, None)
CGImageDestinationFinalize(dest)
# Private Methods
def _create_rgba_bitmap_context(self):
"""Create a new CG Graphics Context."""
color_space = CGColorSpaceCreateDeviceRGB()
pixels_wide = self.width
pixels_high = self.height
bits_per_component = 8
number_of_components = 4 # r,g,b,a
# Convert bits per component to bytes per pixel, rounding up to the next int if necessary
bytes_per_pixel = (bits_per_component * number_of_components + 7)/8
bitmap_bytes_per_row = (pixels_wide * bytes_per_pixel);
# Create an RGB bitmap, let CoreGraphics deal with allocating and managing memory
ctx = CGBitmapContextCreate(None, pixels_wide, pixels_high, bits_per_component, bitmap_bytes_per_row, color_space, kCGImageAlphaPremultipliedLast)
return ctx
\ No newline at end of file
#!/usr/bin/python #!/usr/bin/python
from Quartz import * import canvas
from Cocoa import *
from LaunchServices import * # for kUTTypePNG
import math import math
import os import os
import shutil import shutil
...@@ -11,51 +9,22 @@ size = 512 ...@@ -11,51 +9,22 @@ size = 512
well = "A1" well = "A1"
tileNum = 6 tileNum = 6
def createContext(): def create_canvas():
cs = CGColorSpaceCreateDeviceRGB () return canvas.PlateWellCanvas(size, size)
pixelsWide = size
pixelsHigh = size def write_to_png_file(canvas, filename):
bits_per_component = 8 canvas.write_png_file(filename)
number_of_components = 4 # r,g,b,a
# Convert bits per component to bytes per pixel, rounding up to the next int if necessary def drawRect(canvas, r, g, b, start, isUnfilled = 0):
bytes_per_pixel = (bits_per_component * number_of_components + 7)/8 canvas.draw_inset_rect(r, g, b, start, isUnfilled)
bitmapBytesPerRow = (pixelsWide * bytes_per_pixel);
bitmapByteCount = (bitmapBytesPerRow * pixelsHigh);
# Create an RGB bitmap, let CoreGraphics deal with allocating and managing memory
ctx = CGBitmapContextCreate(None, pixelsWide, pixelsHigh, bits_per_component, bitmapBytesPerRow, cs, kCGImageAlphaPremultipliedLast)
return ctx
def write_to_png_file(ctx, filename):
image = CGBitmapContextCreateImage(ctx)
fileUrl = NSURL.fileURLWithPath_(filename)
dest = CGImageDestinationCreateWithURL(fileUrl, kUTTypePNG, 1, None);
CGImageDestinationAddImage(dest, image, None);
CGImageDestinationFinalize(dest);
def drawRect(ctx, r, g, b, start, isUnfilled = 0):
with CGSavedGState(ctx):
CGContextSetRGBStrokeColor(ctx, r, g, b, 1)
if isUnfilled:
CGContextSetRGBFillColor(ctx, 0, 0, 0, 0) # transparent
else:
CGContextSetRGBFillColor(ctx, r, g, b, 1)
CGContextSetLineWidth(ctx, 40)
CGContextAddRect(ctx, CGRectMake(start, start, size - 2 * start, size - 2 * start))
CGContextDrawPath(ctx, kCGPathFillStroke)
def drawText(ctx, x, y, text): def drawText(canvas, x, y, text):
with CGSavedGState(ctx): canvas.draw_text(x, y, text)
CGContextSetRGBStrokeColor(ctx, 0, 0, 0, 1) # black
CGContextSetRGBFillColor(ctx, 1, 1, 1, 1) # white
CGContextSetTextMatrix(ctx, CGAffineTransformIdentity)
CGContextSelectFont(ctx, "Helvetica Neue", 70, kCGEncodingMacRoman)
CGContextSetTextDrawingMode(ctx, kCGTextFillStroke)
CGContextShowTextAtPoint(ctx, x, y, text, len(text))
def drawMatrix(coordsList, dir, channel, isOverlay): def drawMatrix(coordsList, dir, channel, isOverlay):
nonemptyTiles = set([ calcTile(coords) for coords in coordsList ]) nonemptyTiles = set([ calcTile(coords) for coords in coordsList ])
for tile in range(1, 10): for tile in range(1, 10):
c = createContext() c = create_canvas()
if tile in nonemptyTiles: if tile in nonemptyTiles:
if not isOverlay: if not isOverlay:
drawRect(c, 0, 0, 0, 0) drawRect(c, 0, 0, 0, 0)
...@@ -96,7 +65,7 @@ def overlayTests(sampleCode): ...@@ -96,7 +65,7 @@ def overlayTests(sampleCode):
def save(dir, filename, text, r, g, b, merged = 0): def save(dir, filename, text, r, g, b, merged = 0):
c = createContext() c = create_canvas()
drawRect(c, 0, 0, 0, 0) # fill with black drawRect(c, 0, 0, 0, 0) # fill with black
zero = 0 zero = 0
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment