def test_locale_independence(self): # Ensure that >ASCII Unicode text is decoded correctly regardless of # the locale. text = u'\N{GREEK SMALL LETTER MU}' with locale_context(locale.LC_CTYPE, ('en', 'UTF-8')): gc = agg.GraphicsContextArray((200, 200)) f = Font('modern') with gc: gc.set_font(f) gc.translate_ctm(50, 50) tx0, _, _, _ = gc.get_text_extent(text) gc.show_text(text) x0, _ = gc.get_text_position() with locale_context(locale.LC_CTYPE, ('en', 'ASCII')): gc = agg.GraphicsContextArray((200, 200)) f = Font('modern') with gc: gc.set_font(f) gc.translate_ctm(50, 50) tx1, _, _, _ = gc.get_text_extent(text) gc.show_text(text) x1, _ = gc.get_text_position() self.assertEqual(tx1, tx0) self.assertEqual(x1, x0)
def generic_timing(self, scheme, size): gc = agg.GraphicsContextArray(size, pix_format="bgra32") desired = solid_bgra32(size, self.color) img = agg.GraphicsContextArray(desired, pix_format="bgra32", interpolation=scheme) print("{!r} interpolation, ".format(scheme), bench(lambda: gc.draw_image(img)))
def test_set_get_text_position(self): #print 'testing text position' gc = agg.GraphicsContextArray((5, 5)) gc.set_text_position(1, 1) actual = gc.get_text_position() desired = (1, 1) self.assertTrue(allclose(actual, desired))
def main4(): """ Test drawing an rgb24 into a bgra32""" from PIL import Image pil_img = Image.open('doubleprom_soho_full.jpg') img = fromstring(pil_img.tostring(), UInt8) img = img.resize((pil_img.size[1], pil_img.size[0], 3)) print('typecode:', typecode(img), iscontiguous(img)) print(shape(img)) agg_img = agg.Image(img, "rgb24", interpolation_scheme="simple") gc = agg.GraphicsContextArray((1000, 1000)) N = 1 t1 = time.clock() for i in range(N): with gc: #gc.rotate_ctm(.2) #gc.set_alpha(0.5) gc.draw_image(agg_img) #print pil_img.getpixel((300,300)), img[300,300], gc.bmp_array[300,300] gc.translate_ctm(150, 300) gc.scale_ctm(10, 10) gc.set_fill_color((0.0, 0, 1.0, .5)) #gc.show_text("SUN") t2 = time.clock() print("images per second: %g" % (N / (t2 - t1))) gc.save('sun2.bmp')
def test_antialias(self): gc = agg.GraphicsContextArray((200, 50), pix_format="bgra32") gc.set_antialias(1) f = Font("modern") gc.set_font(f) gc.show_text("hello") save(gc)
def test_move_to(self): gc = agg.GraphicsContextArray((100, 100)) gc.move_to(1.0, 1.0) path = gc._get_path() actual, flag = path._vertex() desired = array((1.0, 1.0)) self.assertTrue(allclose(actual, desired))
def test_set_text_matrix_ndarray(self): """ Test that gc.set_text_matrix accepts 3x3 ndarrays. """ gc = agg.GraphicsContextArray((5, 5)) m = array([[1.0, 2.0, 0.0], [3.0, 4.0, 0.0], [5.0, 6.0, 1.0]]) gc.set_text_matrix(m) m2 = gc.get_text_matrix() self.assertEqual(m2, agg.AffineMatrix(1.0, 2.0, 3.0, 4.0, 5.0, 6.0))
def benchmark_symbols_all_at_once(n_pts=1000, sz=(1000, 1000)): """ Renders all the symbols. """ width, height = sz pts = stats.norm.rvs(size=(n_pts, 2)) * array(sz) / 8. + array(sz) / 2. star_path = agg.CompiledPath() star_path.lines(circle_array()) gc = agg.GraphicsContextArray(sz) gc.set_fill_color((1.0, 0.0, 0.0, 0.1)) gc.set_stroke_color((0.0, 1.0, 0.0, 0.6)) path = agg.CompiledPath() t1 = time.clock() for x, y in pts: path.save_ctm() path.translate_ctm(x, y) path.add_path(star_path) path.restore_ctm() gc.add_path(path) t2 = time.clock() gc.draw_path() t3 = time.clock() gc.save("benchmark_symbols2.bmp") build_path_time = t2 - t1 render_path_time = t3 - t2 tot_time = t3 - t1 print('star count, tot,building path, rendering path:', n_pts, \ tot_time, build_path_time,render_path_time) return
def test_no_antialias(self): gc = agg.GraphicsContextArray((200, 50), pix_format="bgra32") f = Font('modern') gc.set_font(f) gc.set_antialias(0) gc.show_text("hello") save(gc, test_name() + '.bmp')
def main3(): from PIL import Image pil_img = Image.open('doubleprom_soho_full.jpg') img = fromstring(pil_img.tostring(), UInt8) img = 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() print('typecode:', typecode(img), iscontiguous(img)) print(shape(img)) agg_img = agg.Image(img, "bgra32", interpolation_scheme="simple") gc = agg.GraphicsContextArray((760, 760)) N = 100 gc.show_text("SUN") t1 = time.clock() for i in range(N): with gc: #gc.rotate_ctm(.2) gc.set_alpha(1.0) gc.draw_image(agg_img) #print pil_img.getpixel((300,300)), img[300,300], gc.bmp_array[300,300] gc.translate_ctm(150, 300) gc.scale_ctm(10, 10) gc.set_fill_color((0.0, 0, 1.0, .25)) #gc.show_text("SUN") t2 = time.clock() print("images per second: %g" % (N / (t2 - t1))) gc.save('sun3.bmp')
def main(): sz = (1000, 1000) t1 = now() path_and_color, size, center = get_lion() t2 = now() print(t2 - t1) gc = agg.GraphicsContextArray(sz) t1 = now() gc.translate_ctm(sz[0] / 2., sz[1] / 2.) Nimages = 90 for i in range(Nimages): for path, color in path_and_color: gc.begin_path() gc.add_path(path) gc.set_fill_color(color) gc.set_alpha(0.3) gc.fill_path() gc.rotate_ctm(1) t2 = now() print('total time, sec/image, img/sec:', t2 - t1, (t2 - t1) / Nimages, Nimages / (t2 - t1)) gc.save('lion.bmp')
def test_add_path(self): path1 = agg.CompiledPath() path1.move_to(1.0, 1.0) path1.translate_ctm(1.0, 1.0) path1.line_to(2.0, 2.0) # actually (3.0,3.0) path1.scale_ctm(2.0, 2.0) path1.line_to(2.0, 2.0) # actually (5.0,5.0) gc = agg.GraphicsContextArray((100, 100)) gc.move_to(1.0, 1.0) gc.translate_ctm(1.0, 1.0) gc.line_to(2.0, 2.0) # actually (3.0,3.0) sub_path = agg.CompiledPath() sub_path.scale_ctm(2.0, 2.0) sub_path.line_to(2.0, 2.0) gc.add_path(sub_path) path2 = gc._get_path() desired = path1._vertices() actual = path2._vertices() self.assertTrue(allclose(actual, desired)) desired = path1.get_ctm() actual = path2.get_ctm() self.assertEqual(actual, desired)
def test_context_manager_nested(self): gc = agg.GraphicsContextArray((100, 100)) # Set some values. gc.set_stroke_color((1, 0, 0, 1)) gc.set_antialias(0) gc.set_alpha(0.25) with gc: # Change the values in the current context. gc.set_stroke_color((0, 0, 1, 1)) self.assertTrue(all(gc.get_stroke_color() == (0, 0, 1, 1))) gc.set_antialias(1) self.assertEqual(gc.get_antialias(), 1) gc.set_alpha(0.75) self.assertEqual(gc.get_alpha(), 0.75) with gc: # Change the values in the current context. gc.set_stroke_color((1, 0, 1, 1)) self.assertTrue(all(gc.get_stroke_color() == (1, 0, 1, 1))) gc.set_antialias(0) self.assertEqual(gc.get_antialias(), 0) gc.set_alpha(1.0) self.assertEqual(gc.get_alpha(), 1.0) # Verify that we are back to the previous settings. self.assertTrue(all(gc.get_stroke_color() == (0, 0, 1, 1))) self.assertEqual(gc.get_antialias(), 1) self.assertEqual(gc.get_alpha(), 0.75) # Verify that we are back to the previous settings. self.assertTrue(all(gc.get_stroke_color() == (1, 0, 0, 1))) self.assertEqual(gc.get_antialias(), 0) self.assertEqual(gc.get_alpha(), 0.25)
def test_clip_to_rect(self): gc = agg.GraphicsContextArray((10, 10)) gc.move_to(0, 0) gc.line_to(10, 10) gc.clip_to_rect(5, 5, 5, 5) gc.stroke_path() # make sure nothing was drawn in the corner self.assertEqual(gc.bmp_array[-1, 0, 0], 255)
def generic_sun(self, scheme): img = sun(scheme) sz = array((img.width(), img.height())) scaled_sz = sz * .3 scaled_rect = (0, 0, scaled_sz[0], scaled_sz[1]) gc = agg.GraphicsContextArray(tuple(scaled_sz), pix_format="bgra32") gc.draw_image(img, scaled_rect) return gc
def test_stroke_path(self): gc = agg.GraphicsContextArray((5, 5)) gc.move_to(0, 0) gc.line_to(5, 5) gc.stroke_path() # assert the lower left and upper corner are the same, # and have something drawn in them. self.assertEqual(gc.bmp_array[-1, 0, 0], gc.bmp_array[0, -1, 0]) self.assertNotEqual(gc.bmp_array[-1, 0, 0], 255)
def test_begin_path(self): gc = agg.GraphicsContextArray((100, 100)) gc.move_to(1.0, 1.0) gc.begin_path() path = gc._get_path() pt, flag = path._vertex() # !! should get this value from the agg enum value desired = 0 self.assertEqual(flag, desired)
def test_concat_ctm(self): gc = agg.GraphicsContextArray((100, 100)) gc.translate_ctm(2.0, 2.0) m0 = agg.scaling_matrix(2.0, 2.0) gc.concat_ctm(m0) actual = gc.get_ctm() m0.multiply(agg.translation_matrix(2.0, 2.0)) desired = m0 self.assertEqual(actual, tuple(desired))
def sun(interpolation_scheme="simple"): path = os.path.join(os.path.dirname(__file__), 'doubleprom_soho_full.jpg') pil_img = Image.open(path) 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 agg.GraphicsContextArray(img, "bgra32", interpolation_scheme)
def test_dash(self): gc = agg.GraphicsContextArray((100, 100)) gc.set_line_dash([2, 2]) for i in range(10): gc.move_to(0, 0) gc.line_to(0, 100) gc.stroke_path() gc.translate_ctm(10, 0) save(gc, "dash.bmp")
def test_get_set_font(self): gc = agg.GraphicsContextArray((5, 5)) font1 = Font("modern") gc.set_font(font1) font3 = gc.get_font() self.assertEqual(font1.face_name, font3.name) self.assertEqual(font1.size, font3.size) self.assertEqual(font1.family, font3.family) self.assertEqual(font1.style, font3.style) self.assertEqual(font1.encoding, font3.encoding)
def draw_image(self, img, rect=None): """ draw_image(img_gc, rect=(x, y, w, h)) Draws another gc into this one. If 'rect' is not provided, then the image gc is drawn into this one, rooted at (0, 0) and at full pixel size. If 'rect' is provided, then the image is resized into the (w, h) given and drawn into this GC at point (x, y). img_gc is either a Numeric array (WxHx3 or WxHx4) or a GC from Kiva's Agg backend (kiva.agg.GraphicsContextArray). Requires the Python Imaging Library (PIL). """ # We turn img into a PIL object, since that is what ReportLab # requires. To do this, we first determine if the input image # GC needs to be converted to RGBA/RGB. If so, we see if we can # do it nicely (using convert_pixel_format), and if not, we do # it brute-force using Agg. from reportlab.lib.utils import ImageReader from kiva import agg from kiva.compat import pilfromstring, piltostring if type(img) == type(array([])): # Numeric array converted_img = agg.GraphicsContextArray(img, pix_format='rgba32') format = 'RGBA' elif isinstance(img, agg.GraphicsContextArray): if img.format().startswith('RGBA'): format = 'RGBA' elif img.format().startswith('RGB'): format = 'RGB' else: converted_img = img.convert_pixel_format('rgba32', inplace=0) format = 'RGBA' else: warnings.warn("Cannot render image of type %r into PDF context." % type(img)) return # converted_img now holds an Agg graphics context with the image pil_img = pilfromstring(format, (converted_img.width(), converted_img.height()), piltostring(converted_img.bmp_array)) if rect is None: rect = (0, 0, img.width(), img.height()) # Draw the actual image. # Wrap it in an ImageReader object, because that's what reportlab # actually needs. self.gc.drawImage(ImageReader(pil_img), rect[0], rect[1], rect[2], rect[3])
def test_quad_curve_to(self): gc = agg.GraphicsContextArray((100, 100)) ctrl = 1.0, 1.0 to = 2.0, 2.0 gc.quad_curve_to(ctrl[0], ctrl[1], to[0], to[1]) path = gc._get_path() actual_ctrl, flag = path._vertex() self.assertEqual(actual_ctrl, ctrl) self.assertEqual(flag, 3) actual_to, flag = path._vertex() self.assertEqual(actual_to, to) self.assertEqual(flag, 3)
def test_save_restore_state_for_ctm(self): gc = agg.GraphicsContextArray((100, 100)) m0 = agg.translation_matrix(10.0, 10.0) gc.set_ctm(m0) gc.save_state() m1 = agg.translation_matrix(5.0, 5.0) gc.set_ctm(m1) m2 = gc.get_ctm() self.assertEqual(tuple(m1), m2) gc.restore_state() m3 = gc.get_ctm() self.assertEqual(tuple(m0), m3)
def test_save_restore_state_for_ttm(self): # The interesting thing here is that we are verifying # that the text transform matrix (TTM) is *not* saved # with the graphics state. gc = agg.GraphicsContextArray((100, 100)) m0 = agg.translation_matrix(10.0, 10.0) gc.set_text_matrix(m0) gc.save_state() gc.set_text_matrix(agg.translation_matrix(5.0, 5.0)) gc.restore_state() m1 = gc.get_text_matrix() self.assertNotEqual(m1, m0)
def do_check_format(self, fmt): # FIXME: gc = agg.GraphicsContextArray((2, 2), fmt) gc.set_stroke_color((1.0, 0.0, 0.0)) gc.move_to(0.0, 0.5) gc.line_to(2.0, 0.5) gc.stroke_path() gc.save(fmt + ".png") img = agg.Image(fmt + ".png") os.unlink(fmt + ".png") self.assertEqual(list(ravel(img.bmp_array)), self.format_output_map[fmt])
def test_rect(self): gc = agg.GraphicsContextArray((100, 100)) gc.rect(1.0, 1.0, 1.0, 1.0) actual = gc._get_path()._vertices() desired = array(( (1.0, 1.0, agg.path_cmd_move_to, agg.path_flags_none), (1.0, 2.0, agg.path_cmd_line_to, agg.path_flags_none), (2.0, 2.0, agg.path_cmd_line_to, agg.path_flags_none), (2.0, 1.0, agg.path_cmd_line_to, agg.path_flags_none), (0.0, 0.0, agg.path_cmd_end_poly, agg.path_flags_close), (0.0, 0.0, agg.path_cmd_stop, agg.path_flags_none), )) self.assertTrue(allclose(actual, desired))
def main2(): from PIL import Image pil_img = Image.open('doubleprom_soho_full.jpg') img = fromstring(pil_img.tostring(), UInt8) img = 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() print('typecode:', typecode(img), iscontiguous(img)) print(shape(img)) gc = agg.GraphicsContextArray((1000, 1000)) gc.draw_image(img) print(pil_img.getpixel((300, 300)), img[300, 300], gc.bmp_array[300, 300]) gc.save('sun.bmp')
def base_lines(self, lines): gc = agg.GraphicsContextArray((100, 100)) gc.move_to(1.0, 1.0) gc.line_to(2.0, 2.0) # actually (3.0,3.0) gc.lines(lines) actual = gc._get_path()._vertices() desired = array(( (1.0, 1.0, agg.path_cmd_move_to, agg.path_flags_none), (2.0, 2.0, agg.path_cmd_line_to, agg.path_flags_none), (3.0, 3.0, agg.path_cmd_move_to, agg.path_flags_none), (4.0, 4.0, agg.path_cmd_line_to, agg.path_flags_none), (0.0, 0.0, agg.path_cmd_stop, agg.path_flags_none), )) self.assertTrue(allclose(actual, desired))
def test_rotate(self): text = "hello" gc = agg.GraphicsContextArray((150, 150), pix_format="bgra32") f = Font('modern') gc.set_font(f) tx, ty, sx, sy = bbox = gc.get_text_extent(text) gc.translate_ctm(25, 25) gc.rotate_ctm(pi / 2.) gc.translate_ctm(0, -sy) #gc.show_text(text) gc.set_stroke_color([1, 0, 0]) gc.set_fill_color([.5, .5, .5]) gc.rect(tx, ty, sx, sy) gc.stroke_path() gc.show_text(text) save(gc, test_name() + '.bmp')