def compiled_path(): # Creating the compiled path star_points = [ (-20, -30), (0, 30), (20, -30), (-30, 10), (30, 10), (-20, -30), ] path = CompiledPath() path.move_to(*star_points[0]) for pt in star_points[1:]: path.line_to(*pt) locs = [ (100, 100), (100, 300), (100, 500), (200, 100), (200, 300), (200, 500), ] gc = GraphicsContext((300, 600)) gc.set_stroke_color((0, 0, 1, 1)) gc.draw_path_at_points(locs, path, STROKE) with tempfile.NamedTemporaryFile(suffix=".jpg") as fid: gc.save(fid.name) image = Image.from_file(fid.name, resist_width="weak", resist_height="weak") return image
def draw_wire_connections_at_points(gc, points): """ Draw wire connections at each of the given points. This function checks if the graphics context implements optimized methods for doing so, and draws using the most optimal approach available. Parameters ---------- gc : GraphicsContext The Graphics context doing the drawing points : List of pairs of 2-tuple The points where wire connections are to be drawn """ if hasattr(gc, 'draw_marker_at_points'): gc.draw_marker_at_points(points, 4.0, CIRCLE_MARKER) else: wire_connection_path = CompiledPath() wire_connection_path.move_to(0, 0) wire_connection_path.arc(0, 0, 4, 0, tau) if hasattr(gc, 'draw_path_at_points'): gc.draw_path_at_points(points, wire_connection_path, FILL) else: for point in points: with gc: gc.translate_ctm(point[0], point[1]) gc.add_path(wire_connection_path) gc.fill_path()
def create_resistor_path(): """ Creates a CompiledPath for a resistor which can then be re-used as needed. Returns ------- CompiledPath The reistor compiled path """ resistor_path = CompiledPath() resistor_path.move_to(0, 0) resistor_path_points = [(i * 10 + 5, 10 * (-1)**i) for i in range(8)] for x, y in resistor_path_points: resistor_path.line_to(x, y) resistor_path.line_to(80, 0) return resistor_path
def compiled_path(): # Creating the compiled path star_points = [(-20, -30), (0, 30), (20, -30), (-30, 10), (30, 10), (-20, -30)] path = CompiledPath() path.move_to(*star_points[0]) for pt in star_points[1:]: path.line_to(*pt) locs = [(100, 100), (100, 300), (100, 500), (200, 100), (200, 300), (200, 500)] gc = GraphicsContext((300, 600)) gc.set_stroke_color((0, 0, 1, 1)) gc.draw_path_at_points(locs, path, STROKE) file_path = tempfile.mktemp(suffix='.jpg') gc.save(file_path) return file_path
from numpy import array from kiva.image import GraphicsContext, CompiledPath from kiva.constants import STROKE, FILL_STROKE cross = CompiledPath() cross.scale_ctm(10.0, 10.0) lines = array([(0,1),(0,2),(1,2),(1,3),(2,3),(2,2),(3,2),(3,1),(2,1), (2,0),(1,0),(1,1), (0,1)]) cross.lines(lines) gc = GraphicsContext((400,400)) gc.set_stroke_color((1,0,0,1)) gc.draw_path_at_points(array([(50,50), (200,50), (50,200), (200,200)]), cross, STROKE) gc.save("compiled_path.png")
# CompiledPath should always be imported from the same backend as the # GC you are using. In this case, we are using the image GraphicsContext # so we can save to disk when we're done, so we grab the CompiledPath # from there as well. from kiva.image import GraphicsContext, CompiledPath from kiva.constants import STROKE star_points = [(-20,-30), (0, 30), (20,-30), (-30,10), (30,10), (-20,-30)] path = CompiledPath() path.move_to(*star_points[0]) for pt in star_points[1:]: path.line_to(*pt) locs = [(100,100), (100,300), (100,500), (200,100), (200,300), (200,500)] gc = GraphicsContext((300,600)) gc.set_stroke_color((0,0,1,1)) gc.draw_path_at_points(locs, path, STROKE) gc.save("compiled_path.png")