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)
Exemple #3
0
    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 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)
Exemple #5
0
 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 check_agg_mem_leak(self):
     pre = get_mem_usage()
     gc = GraphicsContext((500, 500))
     del gc
     garbagecollector.collect()
     post = get_mem_usage()
     assert (pre == post)
Exemple #7
0
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
Exemple #8
0
    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 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 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 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)
Exemple #12
0
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
Exemple #13
0
    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 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)
Exemple #15
0
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
Exemple #16
0
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
Exemple #17
0
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)
    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
Exemple #18
0
    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
Exemple #19
0
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
Exemple #20
0
    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, :])
Exemple #21
0
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
Exemple #22
0
    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
Exemple #23
0
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
Exemple #24
0
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
Exemple #25
0
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.0)
                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()
    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
Exemple #26
0
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 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")
Exemple #28
0
# 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")
Exemple #29
0
 def create_graphics_context(self, width, height):
     return GraphicsContext((width, height))
Exemple #30
0
    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)