def get_block_im(blockDir, fColor=Color(1.0, 0.0, 0.0),
sThick=2, bThick=30, sColor=None):
'''
blockDir: the the direction in which block needs to be created
'''
stPoint = gm.Point(0,0)
enPoint = stPoint + blockDir
pts = get_box_coords(stPoint, enPoint, wThick=bThick)
pt1, pt2, pt3, pt4 = pts
#Create the points for drawing the block.
mnX = min(pt1.x(), pt2.x(), pt3.x(), pt4.x())
mnY = min(pt1.y(), pt2.y(), pt3.y(), pt4.y())
mnPt = gm.Point(mnX, mnY)
pt1, pt2 = pt1 - mnPt, pt2 - mnPt
pt3, pt4 = pt3 - mnPt, pt4 - mnPt
#print pt1, pt2, pt3, pt4
if sColor is None:
sColor = fColor
xSz = int(np.ceil(max(pt1.x(), pt2.x(), pt3.x(), pt4.x())))
ySz = int(np.ceil(max(pt1.y(), pt2.y(), pt3.y(), pt4.y())))
data = np.zeros((ySz, xSz, 4), dtype=np.uint8)
surface = cairo.ImageSurface.create_for_data(data,
cairo.FORMAT_ARGB32, xSz, ySz)
cr = cairo.Context(surface)
#Create a transparent source
cr.set_source_rgba(1.0, 1.0, 1.0, 0.0)
cr.paint()
#Create the border/Mask
cr.move_to(pt1.x(), pt1.y())
cr.line_to(pt2.x(), pt2.y())
cr.line_to(pt3.x(), pt3.y())
cr.line_to(pt4.x(), pt4.y())
cr.line_to(pt1.x(), pt1.y())
cr.set_line_width(sThick)
cr.set_source_rgba(sColor.b, sColor.g, sColor.r, sColor.a)
cr.stroke()
#Fill in the desired color
cr.set_source_rgba(fColor.b, fColor.g, fColor.r, fColor.a)
cr.move_to(pt1.x(), pt1.y())
cr.line_to(pt2.x(), pt2.y())
cr.line_to(pt3.x(), pt3.y())
cr.line_to(pt4.x(), pt4.y())
cr.line_to(pt1.x(), pt1.y())
cr.fill()
return cr, data
评论列表
文章目录