def assertPathsAreProcessed(self, drawable, width=200, height=200): """ Check that all the paths have been compiled and processed. Parameters ---------- drawable : A drawable object that has a draw method. width : int, optional The width of the array buffer (default is 200). height : int, optional The height of the array buffer (default is 200). note :: A drawable that draws nothing will pass this check. """ gc = GraphicsContext((width, height)) drawable.draw(gc) compiled_path = gc._get_path() total_vertices = compiled_path.total_vertices() self.assertEqual( total_vertices, 0, msg='There are {0} vertices in compiled paths {1} that ' 'have not been processed'.format(total_vertices, compiled_path))
def test_simple(self, color): gc = GraphicsContext(self.size, pix_format="bgra32") desired = self.solid_bgra32(self.size, color) img = GraphicsContext(desired, pix_format="bgra32") gc.draw_image(img) actual = gc.bmp_array # for alpha == 1, image should be exactly equal. self.assert_images_equal(desired, actual)
def test_simple(self): for color in [1.0, 0.0, 0.5]: with self.subTest(color=color): desired = self.solid_bgra32(self.size, color) gc = GraphicsContext(self.size, pix_format="bgra32") img = GraphicsContext(desired, pix_format="bgra32") gc.draw_image(img) actual = gc.bmp_array # for alpha == 1, image should be exactly equal. self.assert_images_equal(desired, actual)
def rect(): gc = GraphicsContext((500, 500)) gc.clear() gc.rect(100, 100, 300, 300) gc.draw_path() with tempfile.NamedTemporaryFile(suffix=".bmp") as fid: gc.save(fid.name) image = Image.from_file(fid.name, resist_width="weak", resist_height="weak") return image
def test_draw_24(self): gc = GraphicsContext((256, 128), pix_format='rgb24') self.image_24.draw(gc) # if test is failing, uncomment this line to see what is drawn #gc.save('test_image_draw_24.png') # smoke test: image isn't all white assert_array_equal(gc.bmp_array[..., :3], self.data[..., :3]) gc2 = GraphicsContext((256, 128), pix_format='rgba32') self.image_24.draw(gc2) assert_array_equal(gc2.bmp_array[..., :3], self.data[..., :3])
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 test_image_alpha(self, color): alpha = 0.5 gc = GraphicsContext(self.size, pix_format="bgra32") orig = self.solid_bgra32(self.size, color, alpha) img = GraphicsContext(orig, pix_format="bgra32") gc.draw_image(img) actual = gc.bmp_array gc_background = self.solid_bgra32(self.size, 1.0) orig = self.solid_bgra32(self.size, color, alpha) desired = self.alpha_blend(gc_background, orig) # also, the alpha channel of the image is not copied into the # desination graphics context, so we have to ignore alphas self.assert_images_close( desired[:, :, :-1], actual[:, :, :-1], diff_allowed=slop_allowed)
def check_agg_mem_leak(self): pre = get_mem_usage() gc = GraphicsContext((500, 500)) del gc garbagecollector.collect() post = get_mem_usage() assert (pre == post)
def _get__image(self): if not self.data.flags['C_CONTIGUOUS']: data = self.data.copy() else: data = self.data image_gc = GraphicsContext(data, pix_format=self.format) return image_gc
def sun(self, interpolation_scheme="simple"): pil_img = PILImage.open('doubleprom_soho_full.jpg') img = frombuffer(piltostring(pil_img), UInt8) img.resize((pil_img.size[1], pil_img.size[0], 3)) alpha = ones(pil_img.size, UInt8) * 255 img = concatenate((img[:, :, ::-1], alpha[:, :, newaxis]), -1).copy() return GraphicsContext(img, "bgra32", interpolation_scheme)
def create_mock_gc(self, width, height, methods=()): """ Create an image graphics context that with mocked methods. Parameters ---------- width, height : The size of the graphics context canvas. methods : iterable the methods which are going to be mocked with a Mock object. """ gc = GraphicsContext((int(width), int(height))) gc.clear((0.0, 0.0, 0.0, 0.0)) for method in methods: setattr(gc, method, Mock()) return gc
def test_ambient_plus_image_alpha(self, color): amb_alpha = 0.5 img_alpha = 0.5 gc = GraphicsContext(self.size, pix_format="bgra32") orig = self.solid_bgra32(self.size, color, img_alpha) img = GraphicsContext(orig, pix_format="bgra32") gc.set_alpha(amb_alpha) gc.draw_image(img) actual = gc.bmp_array gc_background = self.solid_bgra32(self.size, 1.0) orig = self.solid_bgra32(self.size, color, img_alpha) desired = self.alpha_blend( gc_background, orig, ambient_alpha=amb_alpha) # alpha blending is approximate, allow channel differences of to 2. self.assert_images_close(desired, actual, diff_allowed=slop_allowed)
def test_rect_scale(self): color = 0.0 orig_sz = (10, 10) img_ary = self.solid_bgra32(orig_sz, color) orig = GraphicsContext(img_ary, pix_format="bgra32") sx, sy = 5, 20 scaled_rect = (0, 0, sx, sy) gc = GraphicsContext((20, 20), pix_format="bgra32") gc.draw_image(orig, scaled_rect) actual = gc.bmp_array desired_sz = (sx, sy) img_ary = self.solid_bgra32(desired_sz, color) img = GraphicsContext(img_ary, pix_format="bgra32") gc = GraphicsContext((20, 20), pix_format="bgra32") gc.draw_image(img) desired = gc.bmp_array self.assert_images_equal(desired, actual)
class CircleAnimation(AbstractAnimatedContext): radius = Range(10., 30.) last_image = Instance(GraphicsContext) def start(self): with self.gc_lock: gc = self.gc gc.set_fill_color((0.5, 0.5, 0.5)) gc.draw_rect((0, 0, gc.width(), gc.height())) self.last_image = GraphicsContext((gc.width(), gc.height())) with self.last_image: self.last_image.draw_image(gc) super(CircleAnimation, self).start() def step(self, frame_count): from math import sin, pi gc = self.gc radius = self.radius x = 128 y = radius + 128 * abs(sin(pi*frame_count/100.)) with gc: # grey background gc.set_fill_color((0.5, 0.5, 0.5)) gc.draw_rect((0, 0, gc.width(), gc.height())) # copy previous image with alpha gc.set_alpha(0.75) gc.draw_image(self.last_image) gc.set_alpha(1.0) # draw a circle gc.set_fill_color((0.5, 0.5, 0.75)) gc.set_stroke_color((1.0, 1.0, 1.0)) gc.arc(x, y, radius, 0, 2*pi) gc.draw_path() with self.last_image: self.last_image.draw_image(gc) def _gc_default(self): gc = GraphicsContext((256, 256)) return gc
def create_mock_gc( self, width, height, methods=()): """ Create an image graphics context that with mocked methods. Parameters ---------- width, height : The size of the graphics context canvas. methods : iterable the methods which are going to be mocked with a Mock object. """ gc = GraphicsContext((int(width), int(height))) gc.clear((0.0, 0.0, 0.0, 0.0)) for method in methods: setattr(gc, method, Mock()) return gc
def start(self): with self.gc_lock: gc = self.gc gc.set_fill_color((0.5, 0.5, 0.5)) gc.draw_rect((0, 0, gc.width(), gc.height())) self.last_image = GraphicsContext((gc.width(), gc.height())) with self.last_image: self.last_image.draw_image(gc) super(CircleAnimation, self).start()
def test_draw_32(self): gc = GraphicsContext((256, 128), pix_format='rgba32') self.image_32.draw(gc) # if test is failing, uncommetn this line to see what is drawn #gc.save('test_image_draw_32.png') # smoke test: image isn't all white # XXX actually compute what it should look like with alpha transfer white_image = np.ones(shape=(256, 128, 4), dtype='uint8') * 255 self.assertFalse(np.array_equal(white_image, gc.bmp_array))
def _make_color_image(self, color_values, width, orientation, direction): """ Returns an image graphics context representing the array of color values (Nx3 or Nx4). The *width* parameter is the width of the colorbar, and *orientation* is the orientation of the plot. """ bmparray = ones((width, color_values.shape[0], color_values.shape[1]))* color_values * 255 if orientation == "v": bmparray = transpose(bmparray, axes=(1,0,2))[::-1] bmparray = bmparray.astype(uint8) img = GraphicsContext(bmparray, "rgba32") return img
def test_draw_stretched(self): gc = GraphicsContext((256, 256), pix_format='rgba32') self.image_32.bounds = [128, 258] self.image_32.position = [128, 0] self.image_32.draw(gc) # if test is failing, uncommetn this line to see what is drawn #gc.save('test_image_draw_stretched.png') # smoke test: image isn't all white # XXX actually compute what it should look like with alpha transfer white_image = np.ones(shape=(256, 256, 4), dtype='uint8') * 255 self.assertFalse(np.array_equal(white_image, gc.bmp_array)) # left half of the image *should* be white assert_array_equal(gc.bmp_array[:, :128, :], white_image[:, :128, :])
def gradient(): gc = GraphicsContext((500, 500)) gc.scale_ctm(1.25, 1.25) draw(gc) with tempfile.NamedTemporaryFile(suffix=".png") as fid: gc.save(fid.name, file_format="png") image = Image.from_file( fid.name, resist_width="weak", resist_height="weak" ) return image
def test_rect_scale_translate(self): color = 0.0 orig_sz = (10, 10) img_ary = self.solid_bgra32(orig_sz, color) orig = GraphicsContext(img_ary, pix_format="bgra32") tx, ty = 5, 10 sx, sy = 5, 20 translate_scale_rect = (tx, ty, sx, sy) gc = GraphicsContext((40, 40), pix_format="bgra32") gc.draw_image(orig, translate_scale_rect) actual = gc.bmp_array desired_sz = (sx, sy) img_ary = self.solid_bgra32(desired_sz, color) img = GraphicsContext(img_ary, pix_format="bgra32") gc = GraphicsContext((40, 40), pix_format="bgra32") gc.translate_ctm(tx, ty) gc.draw_image(img) desired = gc.bmp_array self.assert_images_equal(desired, actual)
def test_ambient_alpha(self, color): orig = self.solid_bgra32(self.size, color) img = GraphicsContext(orig, pix_format="bgra32") gc = GraphicsContext(self.size, pix_format="bgra32") amb_alpha = 0.5 gc.set_alpha(amb_alpha) gc.draw_image(img) actual = gc.bmp_array gc_background = self.solid_bgra32(self.size, 1.0) orig = self.solid_bgra32(self.size, color) desired = self.alpha_blend(gc_background, orig, ambient_alpha=amb_alpha) # alpha blending is approximate, allow channel differences of to 2. self.assert_images_close(desired, actual, diff_allowed=slop_allowed)
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
def create_graphics_context(self, width, height): return GraphicsContext((width, height))
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() with tempfile.NamedTemporaryFile(suffix=".bmp") as fid: gc.save(fid.name) image = Image.from_file( fid.name, resist_width="weak", resist_height="weak" ) return image
star_points = [(-20,-30), (0, 30), (20,-30), (-30,10), (30,10), (-20,-30)] ring_point = (0,35) ring_radius = 5 fill_color = array((200.,184.,106.))/255. point_color = array((.3,.3,.3)) line_color = point_color for i in range(len(star_points)+1): gc = GraphicsContext((800,800)) gc.scale_ctm(8.0,8.0) gc.translate_ctm(50,50) # draw star gc.set_alpha(.5) x,y = star_points[0] gc.move_to(x,y) for x,y in star_points[1:]: gc.line_to(x,y) gc.close_path() gc.set_fill_color(fill_color) gc.get_fill_color() gc.fill_path()
import math from kiva import CAP_ROUND, CAP_SQUARE, JOIN_ROUND from kiva.image import GraphicsContext gc = GraphicsContext((600, 600)) gc.scale_ctm(2, 2) gc.translate_ctm(150, 150) gc.set_stroke_color((0.66, 0.88, 0.66)) gc.set_line_width(7.0) gc.set_line_join(JOIN_ROUND) gc.set_line_cap(CAP_SQUARE) for i in range(0, 12): theta = i * 2 * math.pi / 12.0 with gc: gc.rotate_ctm(theta) gc.translate_ctm(105, 0) gc.set_stroke_color((1 - (i / 12), math.fmod(i / 6, 1), i / 12)) gc.set_line_width(10.0) gc.set_line_cap(CAP_ROUND) gc.rect(0, 0, 25, 25) gc.stroke_path() with gc: gc.rotate_ctm(theta) gc.translate_ctm(20, 0) gc.move_to(0, 0) gc.line_to(80, 0) gc.stroke_path()
import time from kiva.image import GraphicsContext samples = 1 pt = (250, 250) sz = (100, 100) transparent = (1,1,1,0.) gc_img = GraphicsContext((500,500)) #gc_img.clear(transparent) # clear isn't working... gc_img.bmp_array[:] = (255,255,255,0) gc_img.set_fill_color((1,0,0,.5)) gc_img.set_stroke_color((0,0,1)) gc_img.rect(pt[0],pt[1],sz[0],sz[1]) gc_img.draw_path() gc_main = GraphicsContext((500,500)) gc_main.set_fill_color((0,0,1,.5)) gc_main.set_stroke_color((0,1,0)) gc_main.rect(300,300,100,100) gc_main.draw_path() gc_main.clip_to_rect(pt[0],pt[1],sz[0],sz[1]) t1 = time.clock() for i in range(samples): gc_main.draw_image(gc_img) t2 = time.clock() print('with clip', t2 - t1)
from kiva import constants from kiva.image import GraphicsContext 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() gc.save("simple.bmp")
import time from kiva.image import GraphicsContext samples = 1 pt = (250, 250) sz = (100, 100) transparent = (1,1,1,0.) gc_img = GraphicsContext((500,500)) #gc_img.clear(transparent) # clear isn't working... gc_img.bmp_array[:] = (255,255,255,0) gc_img.set_fill_color((1,0,0,.5)) gc_img.set_stroke_color((0,0,1)) gc_img.rect(pt[0],pt[1],sz[0],sz[1]) gc_img.draw_path() gc_main = GraphicsContext((500,500)) gc_main.set_fill_color((0,0,1,.5)) gc_main.set_stroke_color((0,1,0)) gc_main.rect(300,300,100,100) gc_main.draw_path() gc_main.clip_to_rect(pt[0],pt[1],sz[0],sz[1]) t1 = time.clock() for i in range(samples): gc_main.draw_image(gc_img) t2 = time.clock() print('with clip', t2 - t1) gc_main.save("with_clip.bmp")
from kiva.image import GraphicsContext def draw_ellipse(gc, x, y, major, minor, angle): """ Draws an ellipse given major and minor axis lengths. **angle** is the angle between the major axis and the X axis, in radians. """ with gc: gc.translate_ctm(x, y) ratio = float(major) / minor gc.rotate_ctm(angle) gc.scale_ctm(ratio, 1.0) gc.arc(0, 0, minor, 0.0, 2 * pi) gc.stroke_path() gc.move_to(-minor, 0) gc.line_to(minor, 0) gc.move_to(0, -minor) gc.line_to(0, minor) gc.stroke_path() gc = GraphicsContext((300,300)) 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)) gc.rect(95, 95, 10, 10) gc.fill_path() draw_ellipse(gc, 100, 100, 35.0, 25.0, pi / 6) gc.save("ellipse.bmp")
from kiva.image import GraphicsContext from kiva import constants def add_star(gc): gc.begin_path() # star gc.move_to(-20,-30) gc.line_to(0,30) gc.line_to(20,-30) gc.line_to(-30,10) gc.line_to(30,10) gc.close_path() #line at top of star gc.move_to(-10,30) gc.line_to(10,30) 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) gc.save("star1.bmp")
# 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")
from kiva.image import GraphicsContext gc = GraphicsContext((500,500)) gc.set_fill_color( (1, 0, 0) ) gc.rect(100,100,300,300) gc.draw_path() gc.save("simple2_pre.bmp") # directly manipulate the underlying Numeric array. # The color tuple is expressed as BGRA. gc.bmp_array[:100,:100] = (139, 60, 71, 255) gc.save("simple2_post.bmp")
def simple(): gc = GraphicsContext((499, 500)) gc.set_fill_color((0, 0, 0)) gc.rect(99, 100, 300, 300) gc.draw_path() # directly manipulate the underlying Numeric array. # The color tuple is expressed as BGRA. gc.bmp_array[:99, :100] = (139, 60, 71, 255) with tempfile.NamedTemporaryFile(suffix=".bmp") as fid: gc.save(fid.name) image = Image.from_file( fid.name, resist_width="weak", resist_height="weak" ) return image
from numpy import array from kiva.image import GraphicsContext line_color = (0.0,0.0,0.0) fill_color = array((200.,184.,106.))/255. gc = GraphicsContext((900,900)) gc.rect(30,30,850,300) gc.set_fill_color(fill_color) gc.fill_path() gc.set_fill_color((0.,0.,0.,.4)) gc.translate_ctm(50,50) gc.move_to(10,10) gc.line_to(400,400) gc.move_to(410,10) gc.line_to(410,400) gc.line_to(710,400) gc.line_to(550,300) gc.line_to(710,200) gc.line_to(500,10) gc.close_path() gc.rect(750,10,390,390) gc.draw_path() gc.save("sub_path1.bmp") line_color = (0.0,0.0,0.0) fill_color = array((200.,184.,106.))/255.
from kiva import constants from kiva.image import GraphicsContext 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() gc.save("simple.bmp")
for angle in arange(pi / 4, 2 * pi, pi / 4): gc.line_to(radius * cos(angle), radius * sin(angle)) gc.close_path() gc.fill_path() star_points = [(-20, -30), (0, 30), (20, -30), (-30, 10), (30, 10), (-20, -30)] ring_point = (0, 35) ring_radius = 5 fill_color = array((200.0, 184.0, 106.0)) / 255 point_color = array((0.3, 0.3, 0.3)) line_color = point_color for i in range(len(star_points) + 1): gc = GraphicsContext((800, 800)) gc.scale_ctm(8.0, 8.0) gc.translate_ctm(50, 50) # draw star gc.set_alpha(0.5) x, y = star_points[0] gc.move_to(x, y) for x, y in star_points[1:]: gc.line_to(x, y) gc.close_path() gc.set_fill_color(fill_color) gc.get_fill_color() gc.fill_path() gc.set_alpha(0.4)
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) with tempfile.NamedTemporaryFile(suffix=".bmp") as fid: gc.save(fid.name) image = Image.from_file( fid.name, resist_width="weak", resist_height="weak" ) return image
def ellipse(): gc = GraphicsContext((300, 300)) 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)) gc.rect(95, 95, 10, 10) gc.fill_path() draw_ellipse(gc, 100, 100, 35.0, 25.0, pi / 6) file_path = tempfile.mktemp(suffix='.bmp') 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")
from scipy import pi from kiva.image import GraphicsContext def add_star(gc): gc.begin_path() gc.move_to(-20,-30) gc.line_to(0,30) gc.line_to(20,-30) gc.line_to(-30,10) gc.line_to(30,10) gc.close_path() gc.move_to(-10,30) gc.line_to(10,30) 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)