Python cairo.ImageSurface() Examples

The following are 30 code examples of cairo.ImageSurface(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module cairo , or try the search function .
Example #1
Source File: drawing.py    From xy with MIT License 7 votes vote down vote up
def render(self, scale=96/25.4, margin=10, line_width=0.5):
        import cairo
        x1, y1, x2, y2 = self.bounds
        width = int(scale * self.width + margin * 2)
        height = int(scale * self.height + margin * 2)
        surface = cairo.ImageSurface(cairo.FORMAT_RGB24, width, height)
        dc = cairo.Context(surface)
        dc.set_line_cap(cairo.LINE_CAP_ROUND)
        dc.translate(margin, height - margin)
        dc.scale(scale, -scale)
        dc.translate(-x1, -y1)
        dc.set_line_width(line_width)
        dc.set_source_rgb(1, 1, 1)
        dc.paint()
        # dc.arc(0, 0, 3.0 / scale, 0, 2 * math.pi)
        # dc.set_source_rgb(1, 0, 0)
        # dc.fill()
        dc.set_source_rgb(0, 0, 0)
        for path in self.paths:
            dc.move_to(*path[0])
            for x, y in path:
                dc.line_to(x, y)
        dc.stroke()
        return surface 
Example #2
Source File: strokectrls.py    From sk1-wx with GNU General Public License v3.0 7 votes vote down vote up
def generate_dash_bitmap(dash=None):
    if not dash:
        dash = []
    w, h = DASH_BITMAP_SIZE
    surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, w, h)
    ctx = cairo.Context(surface)
    y = round(h / 2.0)

    ctx.set_source_rgba(0.0, 0.0, 0.0, 1.0)
    ctx.set_line_width(DASH_LINE_WIDTH)
    cdash = []
    for item in dash:
        cdash.append(item * DASH_LINE_WIDTH)
    ctx.set_dash(cdash)
    ctx.move_to(0, y)
    ctx.line_to(w, y)
    ctx.stroke()

    return wal.copy_surface_to_bitmap(surface) 
Example #3
Source File: backend_cairo.py    From matplotlib-4-abaqus with MIT License 6 votes vote down vote up
def draw_image(self, gc, x, y, im):
        # bbox - not currently used
        if _debug: print('%s.%s()' % (self.__class__.__name__, _fn_name()))

        im.flipud_out()

        rows, cols, buf = im.color_conv (BYTE_FORMAT)
        surface = cairo.ImageSurface.create_for_data (
                      buf, cairo.FORMAT_ARGB32, cols, rows, cols*4)
        ctx = gc.ctx
        y = self.height - y - rows

        ctx.save()
        ctx.set_source_surface (surface, x, y)
        ctx.paint()
        ctx.restore()

        im.flipud_out() 
Example #4
Source File: grid.py    From 802.11ah-ns3 with GNU General Public License v2.0 6 votes vote down vote up
def layout(self, width):
        self.__width = width
        surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 1, 1)
        ctx = cairo.Context(surface)
        line_height = 0
        total_height = self.__padding
        line_used = self.__padding
        for legend in self.__legends:
            (t_width, t_height) = ctx.text_extents(legend)[2:4]
            item_width = self.__padding + self.__padding + t_width + self.__padding
            item_height = t_height + self.__padding
            if item_height > line_height:
                line_height = item_height
            if line_used + item_width > self.__width:
                line_used = self.__padding + item_width
                total_height += line_height
            else:
                line_used += item_width
            x = line_used - item_width
        total_height += line_height
        self.__height = total_height 
Example #5
Source File: grid.py    From ns3-rdma with GNU General Public License v2.0 6 votes vote down vote up
def layout(self, width):
        self.__width = width
        surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 1, 1)
        ctx = cairo.Context(surface)
        line_height = 0
        total_height = self.__padding
        line_used = self.__padding
        for legend in self.__legends:
            (t_width, t_height) = ctx.text_extents(legend)[2:4]
            item_width = self.__padding + self.__padding + t_width + self.__padding
            item_height = t_height + self.__padding
            if item_height > line_height:
                line_height = item_height
            if line_used + item_width > self.__width:
                line_used = self.__padding + item_width
                total_height += line_height
            else:
                line_used += item_width
            x = line_used - item_width
        total_height += line_height
        self.__height = total_height 
Example #6
Source File: DescendantsLines.py    From addons-source with GNU General Public License v2.0 6 votes vote down vote up
def size_image(image_path):
    """
    Gets the size of the image

    Need to use a dummy CTX (dctx) as using the final ctx to get this
    information seems to ruin the final image.
    """
    dctx.save()
    iw = 0
    ih = 0
    image = cairo.ImageSurface.create_from_png(image_path)
    if image:
        iw = cairo.ImageSurface.get_width(image)
        ih = cairo.ImageSurface.get_height(image)
    log.debug('Image Size (unscaled): height=%d width=%d', ih, iw)
    dctx.restore()
    return (iw, ih) 
Example #7
Source File: ruler.py    From sk1-wx with GNU General Public License v3.0 6 votes vote down vote up
def paint(self):
        if self.presenter is None:
            return
        w, h = self.dc.get_size()
        fmt = cairo.FORMAT_RGB24
        if self.surface is None or self.width != w or self.height != h:
            self.surface = cairo.ImageSurface(fmt, w, h)
            self.width, self.height = w, h
        self.ctx = cairo.Context(self.surface)
        self.ctx.set_matrix(cairo.Matrix(1.0, 0.0, 0.0, 1.0, 0.0, 0.0))
        self.ctx.set_source_rgb(*config.ruler_bg)
        self.ctx.paint()
        self.ctx.set_antialias(cairo.ANTIALIAS_NONE)
        self.ctx.set_line_width(1.0)
        self.ctx.set_dash([])
        self.ctx.set_source_rgb(*config.ruler_fg)
        if self.vertical:
            self.vrender(w, h)
        else:
            self.hrender(w, h)
        self.dc.draw_surface(self.surface, 0, 0) 
Example #8
Source File: ruler.py    From sk1-wx with GNU General Public License v3.0 6 votes vote down vote up
def load_font(color=(0, 0, 0)):
    fntdir = 'ruler-font%dpx' % config.ruler_font_size
    fntdir = os.path.join(config.resource_dir, 'fonts', fntdir)

    def get_colored_surface(file_name, color, vertical=False):
        file_name = fsutils.get_sys_path(file_name)
        surface = cairo.ImageSurface.create_from_png(file_name)
        w, h = surface.get_width(), surface.get_height()
        res = cairo.ImageSurface(cairo.FORMAT_ARGB32, w, h)
        cr = cairo.Context(res)
        cr.set_source_rgb(*color)
        cr.mask_surface(surface, 0, 0)
        cr.fill()
        return h if vertical else w, res

    for char in '.,-0123456789':
        file_name = 'hdot.png' if char in '.,' else 'h%s.png' % char
        file_name = os.path.join(fntdir, file_name)
        HFONT[char] = get_colored_surface(file_name, color)

        file_name = 'vdot.png' if char in '.,' else 'v%s.png' % char
        file_name = os.path.join(fntdir, file_name)
        VFONT[char] = get_colored_surface(file_name, color, True) 
Example #9
Source File: renderer.py    From sk1-wx with GNU General Public License v3.0 6 votes vote down vote up
def cdc_draw_move_frame(self, trafo):
        bbox = self.presenter.selection.bbox
        if bbox:
            cpath = libcairo.convert_bbox_to_cpath(bbox)
            libcairo.apply_trafo(cpath, trafo)
            libcairo.apply_trafo(cpath, self.canvas.trafo)
            bbox = self.cdc_to_int(*libcairo.get_cpath_bbox(cpath))
            frame = self.cdc_bbox_to_frame(bbox)
            if self.frame and frame == self.frame:
                return
            if not self.frame:
                self.frame = frame
            bbox2 = self.cdc_frame_to_bbox(self.frame)
            frame_sum = self.cdc_bbox_to_frame(libgeom.sum_bbox(bbox, bbox2))
            x, y, w, h = self.cdc_normalize_rect(*frame_sum)
            self.frame = frame
            surface = cairo.ImageSurface(cairo.FORMAT_RGB24, w + 2, h + 2)
            ctx = cairo.Context(surface)
            ctx.set_source_surface(self.surface, -x + 1, -y + 1)
            ctx.paint()
            ctx.set_matrix(cairo.Matrix(1.0, 0.0, 0.0, 1.0, -x + 1, -y + 1))
            self._cdc_draw_cpath(ctx, cpath)
            self.canvas.dc.put_surface(surface, x - 1, y - 1, False)
            self.cdc_reflect_snapping() 
Example #10
Source File: grid.py    From ns3-load-balance with GNU General Public License v2.0 6 votes vote down vote up
def layout(self, width):
        self.__width = width
        surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 1, 1)
        ctx = cairo.Context(surface)
        line_height = 0
        total_height = self.__padding
        line_used = self.__padding
        for legend in self.__legends:
            (t_width, t_height) = ctx.text_extents(legend)[2:4]
            item_width = self.__padding + self.__padding + t_width + self.__padding
            item_height = t_height + self.__padding
            if item_height > line_height:
                line_height = item_height
            if line_used + item_width > self.__width:
                line_used = self.__padding + item_width
                total_height += line_height
            else:
                line_used += item_width
            x = line_used - item_width
        total_height += line_height
        self.__height = total_height 
Example #11
Source File: backend_cairo.py    From Computable with MIT License 6 votes vote down vote up
def draw_image(self, gc, x, y, im):
        # bbox - not currently used
        if _debug: print('%s.%s()' % (self.__class__.__name__, _fn_name()))

        im.flipud_out()

        rows, cols, buf = im.color_conv (BYTE_FORMAT)
        surface = cairo.ImageSurface.create_for_data (
                      buf, cairo.FORMAT_ARGB32, cols, rows, cols*4)
        ctx = gc.ctx
        y = self.height - y - rows

        ctx.save()
        ctx.set_source_surface (surface, x, y)
        ctx.paint()
        ctx.restore()

        im.flipud_out() 
Example #12
Source File: grid.py    From ntu-dsi-dcn with GNU General Public License v2.0 6 votes vote down vote up
def layout(self, width):
        self.__width = width
        surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 1, 1)
        ctx = cairo.Context(surface)
        line_height = 0
        total_height = self.__padding
        line_used = self.__padding
        for legend in self.__legends:
            (t_width, t_height) = ctx.text_extents(legend)[2:4]
            item_width = self.__padding + self.__padding + t_width + self.__padding
            item_height = t_height + self.__padding
            if item_height > line_height:
                line_height = item_height
            if line_used + item_width > self.__width:
                line_used = self.__padding + item_width
                total_height += line_height
            else:
                line_used += item_width
            x = line_used - item_width
        total_height += line_height
        self.__height = total_height 
Example #13
Source File: fontctrl.py    From sk1-wx with GNU General Public License v3.0 6 votes vote down vote up
def generate_fontsample_cache(fonts):
    w = config.font_preview_width
    fontsize = config.font_preview_size
    color = cms.val_255(config.font_preview_color)
    text = config.font_preview_text.decode('utf-8')
    for item in fonts:
        h = libpango.get_sample_size(text, item, fontsize)[1]
        if not h:
            h = 10
            LOG.warn('Incorrect font <%s>: zero font height', item)
        surface = cairo.ImageSurface(cairo.FORMAT_RGB24, w, h)
        ctx = cairo.Context(surface)
        ctx.set_source_rgb(0.0, 0.0, 0.0)
        ctx.paint()
        matrix = cairo.Matrix(1.0, 0.0, 0.0, 1.0, 0.0, 0.0)
        ctx.set_matrix(matrix)
        ctx.set_source_rgb(1.0, 1.0, 1.0)
        ctx.set_antialias(cairo.ANTIALIAS_DEFAULT)
        libpango.render_sample(ctx, text, item, fontsize)
        ctx.fill()
        bmp = wal.copy_surface_to_bitmap(surface)
        FONTSAMPLE_CACHE.append(wal.invert_text_bitmap(bmp, color)) 
Example #14
Source File: strokectrls.py    From sk1-wx with GNU General Public License v3.0 6 votes vote down vote up
def generate_arrow_bitmap(arrow=0, end=False):
    w, h = ARROW_BITMAP_SIZE
    surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, w, h)
    ctx = cairo.Context(surface)
    y = round(h / 2.0)
    x = w / 2

    ctx.set_source_rgba(0.0, 0.0, 0.0, 1.0)
    ctx.set_line_width(ARROW_LINE_WIDTH)
    x = x - 15 if end else x - 15
    ctx.move_to(x, y)
    x2 = w if end else 0
    ctx.line_to(x2, y)
    ctx.stroke()

    if arrow:
        ctx.new_path()
        trafo = [-4, 0.0, 0.0, 4, x, y] if end else [4, 0.0, 0.0, 4, x, y]
        ctx.append_path(arrows.get_arrow_cpath(arrow-1, trafo))
        ctx.fill()

    return wal.copy_surface_to_bitmap(surface) 
Example #15
Source File: backend_cairo.py    From neural-network-animation with MIT License 6 votes vote down vote up
def draw_image(self, gc, x, y, im):
        # bbox - not currently used
        if _debug: print('%s.%s()' % (self.__class__.__name__, _fn_name()))

        im.flipud_out()

        rows, cols, buf = im.color_conv (BYTE_FORMAT)
        surface = cairo.ImageSurface.create_for_data (
                      buf, cairo.FORMAT_ARGB32, cols, rows, cols*4)
        ctx = gc.ctx
        y = self.height - y - rows

        ctx.save()
        ctx.set_source_surface (surface, x, y)
        if gc.get_alpha() != 1.0:
            ctx.paint_with_alpha(gc.get_alpha())
        else:
            ctx.paint()
        ctx.restore()

        im.flipud_out() 
Example #16
Source File: colorctrls.py    From sk1-wx with GNU General Public License v3.0 6 votes vote down vote up
def draw_gradient(self):
        w, h = self.get_size()
        gradient = self.fill[2]
        surface = cairo.ImageSurface(cairo.FORMAT_RGB24, w, h)
        ctx = cairo.Context(surface)
        self.draw_cairo_background(ctx)
        if gradient[0] == sk2const.GRADIENT_LINEAR:
            grd = cairo.LinearGradient(0.0, h / 2.0, w, h / 2.0)
        else:
            grd = cairo.RadialGradient(w / 2.0, h / 2.0, 0,
                                       w / 2.0, h / 2.0, w / 2.0)
        for stop in gradient[2]:
            grd.add_color_stop_rgba(stop[0], *self.get_cairo_color(stop[1]))
        ctx.set_source(grd)
        ctx.rectangle(0, 0, w, h)
        ctx.fill()
        self.gc_draw_bitmap(wal.copy_surface_to_bitmap(surface), 0, 0) 
Example #17
Source File: grid.py    From IEEE-802.11ah-ns-3 with GNU General Public License v2.0 6 votes vote down vote up
def layout(self, width):
        self.__width = width
        surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 1, 1)
        ctx = cairo.Context(surface)
        line_height = 0
        total_height = self.__padding
        line_used = self.__padding
        for legend in self.__legends:
            (t_width, t_height) = ctx.text_extents(legend)[2:4]
            item_width = self.__padding + self.__padding + t_width + self.__padding
            item_height = t_height + self.__padding
            if item_height > line_height:
                line_height = item_height
            if line_used + item_width > self.__width:
                line_used = self.__padding + item_width
                total_height += line_height
            else:
                line_used += item_width
            x = line_used - item_width
        total_height += line_height
        self.__height = total_height 
Example #18
Source File: prefs_ruler.py    From sk1-wx with GNU General Public License v3.0 6 votes vote down vote up
def load_font(self, font_size, color):
        fntdir = 'ruler-font%dpx' % font_size
        fntdir = os.path.join(config.resource_dir, 'fonts', fntdir)

        def get_colored_surface(filename, clr):
            filename = fsutils.get_sys_path(filename)
            surface = cairo.ImageSurface.create_from_png(filename)
            w, h = surface.get_width(), surface.get_height()
            res = cairo.ImageSurface(cairo.FORMAT_ARGB32, w, h)
            cr = cairo.Context(res)
            cr.set_source_rgb(*clr)
            cr.mask_surface(surface, 0, 0)
            cr.fill()
            return w, res

        for char in '.,-0123456789':
            file_name = 'hdot.png' if char in '.,' else 'h%s.png' % char
            file_name = os.path.join(fntdir, file_name)
            self.font[char] = get_colored_surface(file_name, color) 
Example #19
Source File: canvas.py    From sk1-wx with GNU General Public License v3.0 6 votes vote down vote up
def paint(self):
        if not self.matrix: self._fit_to_page()
        self.renderer.set_colorspace(self.printer.colorspace)
        self._keep_center()
        w, h = self.get_size()
        surface = cairo.ImageSurface(cairo.FORMAT_RGB24, w, h)
        self.ctx = cairo.Context(surface)
        self.ctx.set_source_rgb(*wal.wxcolor_to_dec(wal.GRAY))
        self.ctx.paint()
        self.ctx.set_matrix(self.matrix)

        self._draw_page()
        self._render_page()
        self._draw_page_border()

        self.draw_bitmap(wal.copy_surface_to_bitmap(surface)) 
Example #20
Source File: ruler.py    From sk1-wx with GNU General Public License v3.0 6 votes vote down vote up
def paint(self):
        w, h = self.get_size()
        if self.surface is None or self.width != w or self.height != h:
            self.surface = cairo.ImageSurface(cairo.FORMAT_RGB24, w, h)
            self.width, self.height = w, h
        self.ctx = cairo.Context(self.surface)
        self.ctx.set_matrix(cairo.Matrix(1.0, 0.0, 0.0, 1.0, 0.0, 0.0))
        self.ctx.set_source_rgb(*config.ruler_bg)
        self.ctx.paint()
        self.ctx.set_antialias(cairo.ANTIALIAS_NONE)
        self.ctx.set_line_width(1.0)
        self.ctx.set_dash([])
        self.ctx.set_source_rgb(*config.ruler_fg)
        if self.horizontal:
            self.hrender(w, h)
        else:
            self.vrender(w, h)
        self.draw_bitmap(wal.copy_surface_to_bitmap(self.surface)) 
Example #21
Source File: thumbshot.py    From epoptes with GNU General Public License v3.0 6 votes vote down vote up
def thumbshot(width, height):
    """Return a thumbshot of the current screen as bytes."""
    root = Gdk.get_default_root_window()
    if root is None:
        raise RuntimeError('Cannot find the root window, is xorg running?')
    geometry = root.get_geometry()
    surface = cairo.ImageSurface(cairo.FORMAT_RGB24, width, height)
    ctx = cairo.Context(surface)
    # TODO: check if this actually does client-size resizing
    ctx.scale(float(width) / geometry.width, float(height) / geometry.height)
    Gdk.cairo_set_source_window(ctx, root, 0, 0)
    ctx.paint()

    # TODO: is a pixbuf necessary, or can we get the bytes from the surface?
    pixbuf = Gdk.pixbuf_get_from_surface(surface, 0, 0, width, height)
    rowst = pixbuf.get_rowstride()
    pixels = pixbuf.get_pixels()

    return (b"%i\n%ix%i\n" % (rowst, width, height)
            + pixels
            # TODO: the last padding isn't included, so do it manually
            + b"\0"*(rowst*height - len(pixels))) 
Example #22
Source File: DescendantsLines.py    From addons-source with GNU General Public License v2.0 5 votes vote down vote up
def draw_image(image_path, ix, iy, iw, ih, scale_factor):
    # log.debug('Draw Image at x=%d y=%d, w=%d, h=%d, to be scaled by %d',
    # ix, iy, iw, ih, scale_factor)
    ctx.save()
    image = cairo.ImageSurface.create_from_png(image_path)
    if scale_factor != 1.0:
        log.debug('Draw Image: scale factor=%f; result: H=%f, W=%f',
                  scale_factor, ih, iw)
        ctx.scale(scale_factor, scale_factor)
    ctx.set_source_surface(image, (ix + IMAGE_PAD) / scale_factor,
                           (iy + IMAGE_PAD) / scale_factor)
    ctx.paint()
    ctx.restore() 
Example #23
Source File: ft.py    From addons-source with GNU General Public License v2.0 5 votes vote down vote up
def start(
        self,
        fn,
        w,
        h,
        ):
        self.fn = fn
        self.surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, int(w
                 + 1), int(h + 1))
        return self.surface 
Example #24
Source File: DescendantsLines.py    From addons-source with GNU General Public License v2.0 5 votes vote down vote up
def start(self, fn, w, h,):
        self.fn = fn
        if OUTPUT_FMT == 'PNG' or not fn:
            self.surface = cairo.ImageSurface(cairo.FORMAT_ARGB32,
                                              int(w + 1), int(h + 1))
        elif OUTPUT_FMT == 'SVG':
            self.surface = cairo.SVGSurface(self.fn, int(w + 1), int(h + 1))
        elif OUTPUT_FMT == 'PDF':
            self.surface = cairo.PDFSurface(self.fn, int(w + 1), int(h + 1))
        elif OUTPUT_FMT == 'PS':
            self.surface = cairo.PSSurface(self.fn, int(w + 1), int(h + 1))
        else:
            raise AttributeError("no such output format: '%s'" % OUTPUT_FMT)
        return self.surface 
Example #25
Source File: modulargroup.py    From pywonderland with MIT License 5 votes vote down vote up
def main(width, height, depth, xlim=None, ylim=None):
    if xlim is None:
        xlim = [-2, 2]
    if ylim is None:
        ylim = [0, 2]
    surface = cairo.ImageSurface(cairo.FORMAT_RGB24, width, height)
    ctx = HyperbolicDrawing(surface)
    ctx.set_axis(xlim=xlim, ylim=ylim, background_color=(1, 1, 1))
    ctx.set_line_join(2)
    # draw the x-axis
    ctx.move_to(xlim[0], 0)
    ctx.line_to(xlim[1], 0)
    ctx.set_source_rgb(0, 0, 0)
    ctx.set_line_width(0.03)
    ctx.stroke()

    for word, _, triangle in traverse(depth, FUND_DOMAIN):
        if word:
            if word[0] == 'C':
                fc_color = (1, 0.5, 0.75)
            else:
                fc_color = None
        else:
            fc_color = (0.5, 0.5, 0.5)

        ctx.render_domain(triangle, facecolor=fc_color, linewidth=0.04/(len(word)+1))

    surface.write_to_png('modulargroup.png') 
Example #26
Source File: main.py    From pywonderland with MIT License 5 votes vote down vote up
def main(hexagon_size, imgsize):
    surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, imgsize, imgsize)
    ctx = cairo.Context(surface)

    # we will put the center of the hexagon at the origin
    a, b, c = hexagon_size
    ctx.translate(imgsize / 2.0, imgsize / 2.0)
    extent = max(c, a * HALFSQRT3, b * HALFSQRT3) + 1
    ctx.scale(imgsize / (extent * 2.0), -imgsize / (extent * 2.0))
    ctx.translate(-b * HALFSQRT3, -c / 2.0)

    # paint background
    ctx.set_source_rgb(1, 1, 1)
    ctx.paint()
    ctx.set_line_cap(cairo.LINE_CAP_ROUND)
    ctx.set_line_join(cairo.LINE_JOIN_ROUND)

    T = LozengeTiling(hexagon_size)
    sample = run_cftp(T)
    for key, val in T.get_tiles(sample).items():
        for verts in val:
            A, B, C, D = square_to_hex(verts)
            ctx.move_to(A[0], A[1])
            ctx.line_to(B[0], B[1])
            ctx.line_to(C[0], C[1])
            ctx.line_to(D[0], D[1])
            ctx.close_path()
            if key == "T":
                ctx.set_source_rgb(*TOP_COLOR)
            elif key == "L":
                ctx.set_source_rgb(*LEFT_COLOR)
            else:
                ctx.set_source_rgb(*RIGHT_COLOR)
            ctx.fill_preserve()
            ctx.set_line_width(LINE_WIDTH)
            ctx.set_source_rgb(*EDGE_COLOR)
            ctx.stroke()

    surface.write_to_png("random_lozenge_tiling.png") 
Example #27
Source File: backend_cairo.py    From matplotlib-4-abaqus with MIT License 5 votes vote down vote up
def print_png(self, fobj, *args, **kwargs):
        width, height = self.get_width_height()

        renderer = RendererCairo (self.figure.dpi)
        renderer.set_width_height (width, height)
        surface = cairo.ImageSurface (cairo.FORMAT_ARGB32, width, height)
        renderer.set_ctx_from_surface (surface)

        self.figure.draw (renderer)
        surface.write_to_png (fobj) 
Example #28
Source File: fractaltree.py    From pywonderland with MIT License 5 votes vote down vote up
def main():
    surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, WIDTH, HEIGHT)
    ctx = cairo.Context(surface)
    ctx.set_line_cap(cairo.LINE_CAP_ROUND)
    ctx.set_line_join(cairo.LINE_JOIN_ROUND)
    ctx.set_source_rgb(1, 1, 1)
    ctx.paint()
    fractal_tree(ctx, ITERATIONS, ROOT, TRUNK_LEN,
                 RATIO, THETA, ANGLE, PERTURB)
    surface.write_to_png("random_fractal_tree.png") 
Example #29
Source File: grid.py    From ns3-load-balance with GNU General Public License v2.0 5 votes vote down vote up
def layout(self, width):
        surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 1, 1)
        ctx = cairo.Context(surface)

        # calculate scale delta
        data_delta = self.__hi - self.__lo
        closest = 1
        while (closest*10) < data_delta:
            closest *= 10
        if (data_delta / closest) == 0:
            delta = closest
        elif(data_delta / closest) == 1:
            delta = closest / 10
        else:
            delta = closest
        start = self.__lo - (self.__lo % delta) + delta
        end = self.__hi - (self.__hi % delta)

        self.__delta = delta
        self.__width = width

        # calculate text height
        max_text_height = ctx.text_extents("ABCDEFGHIJKLMNOPQRSTUVWXYZabcedefghijklmnopqrstuvwxyz0123456789")[3]
        self.max_text_height = max_text_height
        height = max_text_height + 10
        self.__height = height 
Example #30
Source File: grid.py    From 802.11ah-ns3 with GNU General Public License v2.0 5 votes vote down vote up
def layout(self, width):
        surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 1, 1)
        ctx = cairo.Context(surface)

        # calculate scale delta
        data_delta = self.__hi - self.__lo
        closest = 1
        while (closest*10) < data_delta:
            closest *= 10
        if (data_delta / closest) == 0:
            delta = closest
        elif(data_delta / closest) == 1:
            delta = closest / 10
        else:
            delta = closest
        start = self.__lo - (self.__lo % delta) + delta
        end = self.__hi - (self.__hi % delta)

        self.__delta = delta
        self.__width = width

        # calculate text height
        max_text_height = ctx.text_extents("ABCDEFGHIJKLMNOPQRSTUVWXYZabcedefghijklmnopqrstuvwxyz0123456789")[3]
        self.max_text_height = max_text_height
        height = max_text_height + 10
        self.__height = height