How to use the cairocffi.FONT_WEIGHT_NORMAL function in cairocffi

To help you get started, we’ve selected a few cairocffi examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github kermitt2 / delft / unicodeOCR / traindatagen.py View on Github external
def paint_text(self, text, w, h, rotate=False, ud=False, multi_fonts=False):
        surface = cairo.ImageSurface(cairo.FORMAT_RGB24, w, h)
        with cairo.Context(surface) as context:
            context.set_source_rgb(1, 1, 1)  # White
            context.paint()
            # this font list works in CentOS 7
            if multi_fonts:
                fonts = ['Century Schoolbook', 'Courier', 'STIX', 'URW Chancery L', 'FreeMono']
                context.select_font_face(np.random.choice(fonts), cairo.FONT_SLANT_NORMAL,
                                         np.random.choice([cairo.FONT_WEIGHT_BOLD, cairo.FONT_WEIGHT_NORMAL]))
            else:
                context.select_font_face('Courier', cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_BOLD)
            context.set_font_size(25)
            box = context.text_extents(text)
            border_w_h = (4, 4)
            if box[2] > (w - 2 * border_w_h[1]) or box[3] > (h - 2 * border_w_h[0]):
                raise IOError('Could not fit string into image. Max char count is too large for given image width.')

            # teach the RNN translational invariance by
            # fitting text box randomly on canvas, with some room to rotate
            max_shift_x = w - box[2] - border_w_h[0]
            max_shift_y = h - box[3] - border_w_h[1]
            top_left_x = np.random.randint(0, int(max_shift_x))
            if ud:
                top_left_y = np.random.randint(0, int(max_shift_y))
            else:
github keras-team / keras / examples / image_ocr.py View on Github external
def paint_text(text, w, h, rotate=False, ud=False, multi_fonts=False):
    surface = cairo.ImageSurface(cairo.FORMAT_RGB24, w, h)
    with cairo.Context(surface) as context:
        context.set_source_rgb(1, 1, 1)  # White
        context.paint()
        # this font list works in CentOS 7
        if multi_fonts:
            fonts = [
                'Century Schoolbook', 'Courier', 'STIX',
                'URW Chancery L', 'FreeMono']
            context.select_font_face(
                np.random.choice(fonts),
                cairo.FONT_SLANT_NORMAL,
                np.random.choice([cairo.FONT_WEIGHT_BOLD, cairo.FONT_WEIGHT_NORMAL]))
        else:
            context.select_font_face('Courier',
                                     cairo.FONT_SLANT_NORMAL,
                                     cairo.FONT_WEIGHT_BOLD)
        context.set_font_size(25)
        box = context.text_extents(text)
        border_w_h = (4, 4)
        if box[2] > (w - 2 * border_w_h[1]) or box[3] > (h - 2 * border_w_h[0]):
            raise IOError(('Could not fit string into image.'
                           'Max char count is too large for given image width.'))

        # teach the RNN translational invariance by
        # fitting text box randomly on canvas, with some room to rotate
        max_shift_x = w - box[2] - border_w_h[0]
        max_shift_y = h - box[3] - border_w_h[1]
        top_left_x = np.random.randint(0, int(max_shift_x))
github limbo018 / DREAMPlace / dreamplace / ops / draw_place / PlaceDrawer.py View on Github external
## draw bins 
            #for i in range(1, num_bins_x):
            #    ctx.move_to(normalize_x(bin_xl(i)), normalize_y(yl))
            #    ctx.line_to(normalize_x(bin_xl(i)), normalize_y(yh))
            #    ctx.close_path()
            #    ctx.stroke()
            #for i in range(1, num_bins_y):
            #    ctx.move_to(normalize_x(xl), normalize_y(bin_yl(i)))
            #    ctx.line_to(normalize_x(xh), normalize_y(bin_yl(i)))
            #    ctx.close_path()
            #    ctx.stroke()

            # draw cells
            ctx.set_font_size(16)
            ctx.select_font_face("monospace", cairo.FONT_SLANT_NORMAL, 
                    cairo.FONT_WEIGHT_NORMAL)
            node_xl = x
            node_yl = layout_yl+layout_yh-(y+node_size_y[0:len(y)]) # flip y 
            node_xh = node_xl+node_size_x[0:len(x)]
            node_yh = layout_yl+layout_yh-y # flip y 
            node_xl = normalize_x(node_xl)
            node_yl = normalize_y(node_yl)
            node_xh = normalize_x(node_xh)
            node_yh = normalize_y(node_yh)
            ctx.set_line_width(line_width)
            #print("plot layout")
            # draw fixed macros
            ctx.set_source_rgba(1, 0, 0, alpha=0.5)
            for i in range(num_movable_nodes, num_physical_nodes):
                ctx.rectangle(node_xl[i], node_yl[i], node_xh[i]-node_xl[i], node_yh[i]-node_yl[i])  # Rectangle(xl, yl, w, h)
                ctx.fill()
            ctx.set_source_rgba(0, 0, 0, alpha=1.0)  # Solid color
github ealgis / ealgis / django / ealgis / colour_scale.py View on Github external
# draw the colour scale; two of the levels extend "beyond" (0, 1] so
        # we draw slightly past that range
        v_start = - 1. / (self.nlevels - 2)
        v_end = 1. - v_start
        v_inc = (v_end - v_start) / (legend_height - 1)
        for i in list(range(legend_height)):
            v = v_end - (v_inc * i)
            c = self.lookup(v, normalise=False)
            cr.set_source_rgb(c.r, c.g, c.b)
            y = legend_from + i + 1.5
            cr.move_to(1, y)
            cr.line_to(1 + legend_width, y)
            cr.stroke()
        # draw annotation
        cr.set_source_rgb(0, 0, 0)
        cr.select_font_face("Inconsolata", cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_NORMAL)
        cr.set_font_size(12)
        nanno = self.nanno
        if not self.is_discrete and nanno < 3:
            nanno = 3

        def draw_label(v, text):
            y_attach = legend_from + (v_end - v) / v_inc + 1.5
            # draw anchor line
            cr.move_to(legend_width, y_attach)
            cr.line_to(legend_width + 2 + ATTACH_W, y_attach)
            cr.stroke()
            # draw the text
            (x, y, width, height, dx, dy) = cr.text_extents(text)
            cr.move_to(legend_width + 2 * ATTACH_W, y_attach + height / 2)
            cr.show_text(text)
github Sofwath / thaanaOCR / thaanaocr.py View on Github external
def paint_text(text, w, h, rotate=False, ud=False, multi_fonts=False):
    surface = cairo.ImageSurface(cairo.FORMAT_RGB24, w, h)
    with cairo.Context(surface) as context:
        context.set_source_rgb(1, 1, 1)  # White
        context.paint()
        # this font list for Thaana fonts to be trained on (make sure you have these on your box or change)
        if multi_fonts:
            fonts = ['Faruma' , 'MV Typewriter', 'MV Faseyha', 'MV Iyyu Formal', 'MV Waheed','MV Boli']
            context.select_font_face(np.random.choice(fonts), cairo.FONT_SLANT_NORMAL,
                                     np.random.choice([cairo.FONT_WEIGHT_BOLD, cairo.FONT_WEIGHT_NORMAL]))
        else:
            context.select_font_face('Thaana Unicode Akeh', cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_BOLD)
        context.set_font_size(16)
        text = text[::-1]
        box = context.text_extents(text)
        border_w_h = (4, 4)
        if box[2] > (w - 2 * border_w_h[1]) or box[3] > (h - 2 * border_w_h[0]):
            raise IOError('Could not fit string into image. Max char count is too large for given image width.')

        # teach the RNN translational invariance by
        # fitting text box randomly on canvas, with some room to rotate
        max_shift_x = w - box[2] - border_w_h[0]
        max_shift_y = h - box[3] - border_w_h[1]
        top_left_x = np.random.randint(0, int(max_shift_x))
        if ud:
            top_left_y = np.random.randint(0, int(max_shift_y))
github pannous / tensorflow-ocr / train.py View on Github external
def paint_text(text, w, h, rotate=False, move=False, multi_fonts=False, background=False):
    surface = cairo.ImageSurface(cairo.FORMAT_RGB24, w, h)
    with cairo.Context(surface) as context:
        context.set_source_rgb(1, 1, 1)  # White
        context.paint()
        if multi_fonts:
            # Calibri Century Comic Sans  Courier New Futura Georgia
            fonts = ['Century Schoolbook', 'Courier', 'Arial', 'STIX','Tahoma','Times New Roman','Trebuchet MS',
                     'Verdana','Wide Latin','Calibri','Century','Comic Sans','Courier','New Futura','Georgia',
                     'Lucida','Lucida Console','Magneto','Mistral','URW Chancery L', 'FreeMono','DejaVue Sans Mono']
            font_slant = np.random.choice([cairo.FONT_SLANT_NORMAL,cairo.FONT_SLANT_ITALIC,cairo.FONT_SLANT_OBLIQUE])
            font_weight = np.random.choice([cairo.FONT_WEIGHT_BOLD, cairo.FONT_WEIGHT_NORMAL, cairo.FONT_WEIGHT_NORMAL])
            context.select_font_face(np.random.choice(fonts), font_slant, font_weight)
        else:
            context.select_font_face('Courier', cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_BOLD)
        # context.set_font_size(25)
        font_size = randint(12, 42)
        context.set_font_size(font_size)
        box = context.text_extents(text)
        border_w_h = (font_size/2, font_size/2)
        # if box[2] > (w - 2 * border_w_h[1]) or box[3] > (h - 2 * border_w_h[0]):
        #     raise IOError('Could not fit string into image. Max char count is too large for given image width.')

        # teach the RNN translational invariance by
        # fitting text box randomly on canvas, with some room to rotate
        min_x = 0 #font_size/4
        min_y = 0# font_size/4
        max_shift_x = w - box[2] - border_w_h[0]
github gitenberg-dev / gitberg / gitenberg / util / tenprintcover.py View on Github external
width = cover_width - (2 * cover_height * cover_margin / 100)
        height = title_height
        title_lines, font_height = cover_image.text(title, x, y, width, height, fill, title_font)
        if subtitle:
            y = min(
                y + font_height * title_lines * cover_height,
                title_height - subtitle_font_properties[0]
            )

            cover_image.text(subtitle, x, y, width, height, fill, subtitle_font)

        author_font_size = cover_width * 0.07
        author_font_properties = (
            author_font_size,
            cairo.FONT_SLANT_NORMAL,
            cairo.FONT_WEIGHT_NORMAL
        )
        author_font = cover_image.font(select_font(author), author_font_properties)
        author_height = (cover_height - cover_width - (cover_height * cover_margin / 100)) * 0.25
        x = cover_height * cover_margin / 100
        y = title_height
        width = cover_width - (2 * cover_height * cover_margin / 100)
        height = author_height
        cover_image.text(author, x, y, width, height, fill, author_font)
github mtianyan / NeuralNetworksGetStarted / 7-caffe_and_keras / 7-3 keras-master / examples / image_ocr.py View on Github external
def paint_text(text, w, h, rotate=False, ud=False, multi_fonts=False):
    surface = cairo.ImageSurface(cairo.FORMAT_RGB24, w, h)
    with cairo.Context(surface) as context:
        context.set_source_rgb(1, 1, 1)  # White
        context.paint()
        # this font list works in CentOS 7
        if multi_fonts:
            fonts = ['Century Schoolbook', 'Courier', 'STIX', 'URW Chancery L', 'FreeMono']
            context.select_font_face(np.random.choice(fonts), cairo.FONT_SLANT_NORMAL,
                                     np.random.choice([cairo.FONT_WEIGHT_BOLD, cairo.FONT_WEIGHT_NORMAL]))
        else:
            context.select_font_face('Courier', cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_BOLD)
        context.set_font_size(25)
        box = context.text_extents(text)
        border_w_h = (4, 4)
        if box[2] > (w - 2 * border_w_h[1]) or box[3] > (h - 2 * border_w_h[0]):
            raise IOError('Could not fit string into image. Max char count is too large for given image width.')

        # teach the RNN translational invariance by
        # fitting text box randomly on canvas, with some room to rotate
        max_shift_x = w - box[2] - border_w_h[0]
        max_shift_y = h - box[3] - border_w_h[1]
        top_left_x = np.random.randint(0, int(max_shift_x))
        if ud:
            top_left_y = np.random.randint(0, int(max_shift_y))
        else:
github koonimaru / DeepGMAP / deepgmap / post_train_tools / sequence_visualizer.py View on Github external
Nucpos=0
            Nucneg=0
            Nucsize=max_value*SCALE
            Nucsize2=sum_value*SCALE
            x_pos=k%(DNA_len/line_num)
            #cr.move_to(50+x_pos*40*0.75, y_center)               
            #cr.select_font_face("Sans", cairo.FONT_SLANT_NORMAL,cairo.FONT_WEIGHT_NORMAL)
            #select_color(cr, Nuc)
            #font_mat=cairo.Matrix(xx=40.0,yx=0.0,xy=0.0,yy=Nucsize+0.1,x0=0.0,y0=0.0)
            #cr.set_font_matrix(font_mat)
            #print Nuc
            #cr.show_text(str(Nuc))
            cr.move_to(100+x_pos*40*0.75, y_center)               
            cr.select_font_face("Sans", cairo.FONT_SLANT_NORMAL,cairo.FONT_WEIGHT_NORMAL)
            select_color(cr, Nuc2)
            font_mat=cairo.Matrix(xx=40.0,yx=0.0,xy=0.0,yy=Nucsize+20.0,x0=0.0,y0=0.0)
            cr.set_font_matrix(font_mat)
            cr.show_text(str(Nuc2))    
        #cr.set_font_size(40)
        cr.show_page()
github mgiraldo / tenprintcover-py / tenprintcover.py View on Github external
y = cover_height * cover_margin / 100 * 2
        width = cover_width - (2 * cover_height * cover_margin / 100)
        height = title_height
        title_lines, font_height = cover_image.text(title, x, y, width, height, fill, title_font)
        if subtitle:
            y = min(
                y + font_height * title_lines * cover_height,
                title_height - subtitle_font_properties[0]
            )
            cover_image.text(subtitle, x, y, width, height, fill, subtitle_font)

        author_font_size = cover_width * 0.07
        author_font_properties = (
            author_font_size,
            cairo.FONT_SLANT_NORMAL,
            cairo.FONT_WEIGHT_NORMAL
        )
        author_font = cover_image.font(select_font(author), author_font_properties)
        author_height = (cover_height - cover_width - (cover_height * cover_margin / 100)) * 0.25

        x = cover_height * cover_margin / 100
        y = title_height
        width = cover_width - (2 * cover_height * cover_margin / 100)
        height = author_height
        cover_image.text(author, x, y, width, height, fill, author_font)