def main(): gc = GraphicsContext((500, 500)) gc.scale_ctm(1.25, 1.25) draw(gc) gc.save(splitext(__file__)[0]+'.png', file_format='png')
def rect(): gc = GraphicsContext((500, 500)) gc.clear() gc.rect(100, 100, 300, 300) gc.draw_path() file_path = tempfile.mktemp(suffix='.bmp') gc.save(file_path) return file_path
def save(self, path): """ Save the contents of the component to a file. """ # 100 DPI dimensions of the Macbook Pro 15" size = (1435, 982) gc = GraphicsContext(size) self.component.draw(gc) gc.save(path)
def test_add_to_path(self): path = CompiledPath() path.begin_path() path.move_to(-5, -5) path.line_to(-5, 5) path.line_to(5, 5) path.line_to(5, -5) path.line_to(-5, -5) marker = CustomMarker(path=path) gc = GraphicsContext((50, 50)) # should not fail marker.add_to_path(gc.get_empty_path(), np.int64(0))
def test_bad_image_size(self): arr = np.array([[1, 2], [3, 4]], dtype=np.uint8) gc = GraphicsContext((50, 50)) # The draw_image methods expects its first argument # to be a 3D whose last dimension has length 3 or 4. # Passing in arr should raise a value error. self.assertRaises(ValueError, gc.draw_image, arr) # Pass in a 3D array, but with an invalid size in the last dimension. self.assertRaises(ValueError, gc.draw_image, arr.reshape(2, 2, 1))
def gradient(): gc = GraphicsContext((500, 500)) gc.scale_ctm(1.25, 1.25) draw(gc) file_path = tempfile.mktemp(suffix='.png') gc.save(file_path, file_format='png') return file_path
def main(): gc = GraphicsContext((500, 500)) gc.scale_ctm(1.25, 1.25) draw(gc) gc.save(splitext(__file__)[0] + '.png', file_format='png')
font = Font(face_name="Arial", size=32) gc.set_font(font) gc.translate_ctm(100.0, 100.0) gc.move_to(-5, 0) gc.line_to(5, 0) gc.move_to(0, 5) gc.line_to(0, -5) gc.move_to(0, 0) gc.stroke_path() txtRot = agg.rotation_matrix(PI / 6) gc.set_text_matrix(txtRot) gc.show_text("Hello") txtRot.invert() gc.set_text_matrix(txtRot) gc.show_text("inverted") if __name__ == "__main__": tests = ( (test_clip_stack, "clip_stack.png"), (test_arc_to, "arc_to.png"), (test_handling_text, "handling_text.png"), ) for test_func, filename in tests: img = GraphicsContext((800, 600)) img.clear(WHITE) test_func(img) img.save(filename)
def test_clip_stack(gc): sub_windows = ((10.5, 250, 200, 200), (220.5, 250, 200, 200), (430.5, 250, 200, 200), (10.5, 10, 200, 200), (220.5, 10, 200, 200), (430.5, 10, 200, 200)) gc.set_line_width(2) gc.set_stroke_color(black) gc.rects(sub_windows) gc.stroke_path() img = GraphicsContext((200, 200)) main_rects = ((40.5, 30.5, 120, 50), (40.5, 120.5, 120, 50)) disjoint_rects = ((60.5, 115.5, 80, 15), (60.5, 70.5, 80, 15)) vert_rect = (60.5, 10.5, 55, 180) # Draw the full image draw_sub_image(img, 200, 200) gc.draw_image(img, sub_windows[0]) img.clear() # First clip img.clip_to_rects(main_rects) draw_sub_image(img, 200, 200); gc.draw_image(img, sub_windows[1]) # Second Clip with img: img.clear() img.clip_to_rects(main_rects) img.clip_to_rect(*vert_rect) draw_sub_image(img, 200, 200) gc.draw_image(img, sub_windows[2]) # Pop back to first clip img.clear() draw_sub_image(img, 200, 200) gc.draw_image(img, sub_windows[3]) # Adding a disjoing set of rects img.clear() with img: img.clip_to_rects(main_rects) img.clip_to_rects(disjoint_rects) draw_sub_image(img, 200, 200) gc.draw_image(img, sub_windows[4]) # Pop back to first clip img.clear() draw_sub_image(img, 200, 200) gc.draw_image(img, sub_windows[5])
def test_handling_text(gc): font = Font(face_name="Arial", size = 32) gc.set_font(font) gc.translate_ctm(100.0, 100.0) gc.move_to(-5,0) gc.line_to(5,0) gc.move_to(0,5) gc.line_to(0,-5) gc.move_to(0,0) gc.stroke_path() txtRot = agg.rotation_matrix(PI/6) gc.set_text_matrix(txtRot) gc.show_text("Hello") txtRot.invert() gc.set_text_matrix(txtRot) gc.show_text("inverted") if __name__ == "__main__": tests = ((test_clip_stack, "clip_stack.png"), (test_arc_to, "arc_to.png"), (test_handling_text, "handling_text.png")) for test_func, filename in tests: img = GraphicsContext((800, 600)) img.clear(white) test_func(img) img.save(filename)
def simple(): gc = GraphicsContext((100, 100)) gc.clear() gc.set_line_cap(constants.CAP_SQUARE) gc.set_line_join(constants.JOIN_MITER) gc.set_stroke_color((1, 0, 0)) gc.set_fill_color((0, 0, 1)) gc.rect(0, 0, 30, 30) gc.draw_path() file_path = tempfile.mktemp(suffix='.bmp') gc.save(file_path) return file_path
def test_bad_image_shape(self): arr = np.array([[1, 2], [3, 4]], dtype=np.uint8) gc = GraphicsContext((50, 50)) # Pass in a 3D array, but with an invalid size in the last dimension. self.assertRaises(ValueError, gc.draw_image, arr.reshape(2, 2, 1))
def stars(): gc = GraphicsContext((500, 500)) gc.translate_ctm(250, 300) add_star(gc) gc.draw_path() gc.translate_ctm(0, -100) add_star(gc) gc.set_fill_color((0.0, 0.0, 1.0)) gc.draw_path(constants.EOF_FILL_STROKE) file_path = tempfile.mktemp(suffix='.bmp') gc.save(file_path) return file_path
def stars(): gc = GraphicsContext((500, 500)) with gc: gc.set_alpha(0.3) gc.set_stroke_color((1.0, 0.0, 0.0)) gc.set_fill_color((0.0, 1.0, 0.0)) for i in range(0, 600, 5): with gc: gc.translate_ctm(i, i) gc.rotate_ctm(i * pi / 180.) add_star(gc) gc.draw_path() gc.set_fill_color((0.5, 0.5, 0.5, 0.4)) gc.rect(150, 150, 200, 200) gc.fill_path() file_path = tempfile.mktemp(suffix='.bmp') gc.save(file_path) return file_path
def test_clip_stack(gc): sub_windows = ( (10.5, 250, 200, 200), (220.5, 250, 200, 200), (430.5, 250, 200, 200), (10.5, 10, 200, 200), (220.5, 10, 200, 200), (430.5, 10, 200, 200), ) gc.set_line_width(2) gc.set_stroke_color(BLACK) gc.rects(sub_windows) gc.stroke_path() img = GraphicsContext((200, 200)) main_rects = ((40.5, 30.5, 120, 50), (40.5, 120.5, 120, 50)) disjoint_rects = ((60.5, 115.5, 80, 15), (60.5, 70.5, 80, 15)) vert_rect = (60.5, 10.5, 55, 180) # Draw the full image draw_sub_image(img, 200, 200) gc.draw_image(img, sub_windows[0]) img.clear() # First clip img.clip_to_rects(main_rects) draw_sub_image(img, 200, 200) gc.draw_image(img, sub_windows[1]) # Second Clip with img: img.clear() img.clip_to_rects(main_rects) img.clip_to_rect(*vert_rect) draw_sub_image(img, 200, 200) gc.draw_image(img, sub_windows[2]) # Pop back to first clip img.clear() draw_sub_image(img, 200, 200) gc.draw_image(img, sub_windows[3]) # Adding a disjoing set of rects img.clear() with img: img.clip_to_rects(main_rects) img.clip_to_rects(disjoint_rects) draw_sub_image(img, 200, 200) gc.draw_image(img, sub_windows[4]) # Pop back to first clip img.clear() draw_sub_image(img, 200, 200) gc.draw_image(img, sub_windows[5])
from enable.kiva_graphics_context import GraphicsContext gc = GraphicsContext((500,500)) gc.clear() gc.rect(100,100,300,300) gc.draw_path() gc.save("rect.bmp")
def simple(): gc = GraphicsContext((499, 500)) gc.set_fill_color((0, 0, 0)) gc.rect(99, 100, 300, 300) gc.draw_path() gc.save(tempfile.mktemp(suffix=".bmp")) # directly manipulate the underlying Numeric array. # The color tuple is expressed as BGRA. gc.bmp_array[:99, :100] = (139, 60, 71, 255) file_path = tempfile.mktemp(suffix='.bmp') gc.save(file_path) return file_path