Drawing A Ruler

This script demonstrates how to use the dc module to draw a ruler of 200nm length into a spatial image. For the gloss effect, wxWidgets has to be compiled with the transparency option. Execution gives the following result:

import wx
import dc
import gui

def draw_ruler( image, glossy=False ):
	"Draw a ruler of length 200nm onto the image"
	if not image.IsSpatial():
		return False
	sampling = image.GetSpatialSampling()
	im_size = 2000/sampling[0]
	bmp = wx.EmptyBitmap(im_size,20)	
	the_dc = wx.MemoryDC( bmp )
	the_dc.SetPen( wx.Pen(wx.Colour(0,0,0,0),0,wx.TRANSPARENT) )
	brushes = [ wx.Brush( wx.Colour( 30, 30, 30 ), wx.SOLID ), 
				wx.Brush( wx.Colour( 200, 200, 200 ), wx.SOLID ) ]
				
	for i in range(0,5):
		the_dc.SetBrush( brushes[ i % 2 ] )
		the_dc.DrawRectangle( i*im_size/5, 0, 50, 20 )
	if ( glossy ):
		the_dc.SetBrush(wx.Brush(wx.Colour(255,255,255,30)))
		the_dc.DrawEllipse( -50, -10, im_size+100, 20 )
	wi = bmp.ConvertToImage()
	dc.Blit( wi, im, Point( ( int(image.GetSize().GetWidth()-im_size )/2), 10 ) )
	return True

the_bm = wx.EmptyBitmap(200,20,depth=32)
im = CreateImage( Size( 400, 400 ) )
im.SetSpatialSampling( Vec3( 10, 10, 1 ))
im.ApplyIP( alg.Randomize() )
draw_ruler( im )

v = Viewer( im )

Attachments