Python cairo 模块,FORMAT_ARGB32 实例源码

我们从Python开源项目中,提取了以下19个代码示例,用于说明如何使用cairo.FORMAT_ARGB32

项目:dxf2gcode    作者:cnc-club    | 项目源码 | 文件源码
def cairo_image_surface_from_image(image):
    if image.mode != 'RGBA':
        image = image.convert('RGBA')

    width, height = image.size
    stride = cairo.ImageSurface.format_stride_for_width(
        cairo.FORMAT_ARGB32, width)

    image_buffer = array.array('c')
    image_buffer.fromstring(
        image_rgba_to_bgra(
            image.tostring()))

    cairo_image = cairo.ImageSurface.create_for_data(
        image_buffer, cairo.FORMAT_ARGB32, width, height, stride)

    return cairo_image
项目:godot-themes    作者:Geequlim    | 项目源码 | 文件源码
def svg2png(svg_file, output_file, scale=1):
    # Get the svg files content
    svg_data = open(svg_file).read()

    # Get the width / height inside of the SVG
    doc = minidom.parseString(svg_data)
    width = [path.getAttribute('width') for path in doc.getElementsByTagName('svg')][0]
    height = [path.getAttribute('height') for path in doc.getElementsByTagName('svg')][0]
    width = int(round(float(re.compile('(\d+\.*\d*)\w*').findall(width)[0])))
    height = int(round(float(re.compile('(\d+\.*\d*)\w*').findall(height)[0])))
    doc.unlink()

    # Create the png
    img = cairo.ImageSurface(
        cairo.FORMAT_ARGB32, width * scale, height * scale)
    ctx = cairo.Context(img)
    ctx.scale(scale, scale)
    handler = rsvg.Handle(None, str(svg_data))
    handler.render_cairo(ctx)
    img.write_to_png(output_file)
    print("{} ==> {}".format(svg_file, output_file))
项目:paperwork-backend    作者:openpaperwork    | 项目源码 | 文件源码
def image2surface(img):
    """
    Convert a PIL image into a Cairo surface
    """
    if not CAIRO_AVAILABLE:
        raise Exception("Cairo not available(). image2surface() cannot work.")

    # TODO(Jflesch): Python 3 problem
    # cairo.ImageSurface.create_for_data() raises NotImplementedYet ...

    # img.putalpha(256)
    # (width, height) = img.size
    # imgd = img.tobytes('raw', 'BGRA')
    # imga = array.array('B', imgd)
    # stride = width * 4
    #  return cairo.ImageSurface.create_for_data(
    #      imga, cairo.FORMAT_ARGB32, width, height, stride)

    # So we fall back to this method:
    global g_lock
    with g_lock:
        img_io = io.BytesIO()
        img.save(img_io, format="PNG")
        img_io.seek(0)
        return cairo.ImageSurface.create_from_png(img_io)
项目:pyphy-engine    作者:pulkitag    | 项目源码 | 文件源码
def imprint(self, cr, xSz, ySz):
        '''
            xSz, ySz: Size of the canvas on which imprint has to be made. 
        '''
        #Create Source
        y, x  = self.pos_.y_asint(), self.pos_.x_asint()        
        srcIm = np.zeros((ySz, xSz, 4), dtype=np.uint8)
        #print "pos: (%f, %f), sz:(%f, %f)" % (x, y, self.imSzX_, self.imSzY_)
        srcIm[y : y + self.imSzY_, x : x + self.imSzX_,:] = self.data_.im[:] 
        surface = cairo.ImageSurface.create_for_data(srcIm, 
                                cairo.FORMAT_ARGB32, xSz, ySz)
        cr.set_source_surface(surface)  
        #Create Mask
        pt1, pt2, pt3, pt4 = self.pts_  
        cr.move_to(pt1.x(), pt1.y())
        cr.line_to(pt2.x(), pt2.y())
        cr.line_to(pt3.x(), pt3.y())
        cr.line_to(pt4.x(), pt4.y())
        cr.line_to(pt1.x(), pt1.y())
        #Fill source into the mask
        cr.fill()
项目:pyphy-engine    作者:pulkitag    | 项目源码 | 文件源码
def imprint(self, cr, xSz, ySz):
        '''
            xSz, ySz: Arena Size
        '''
        #Get the position of bottom left corner.
        y, x  = self.pos_.y_asint() - self.yOff_, self.pos_.x_asint() - self.xOff_
        #If the ball is outside the arena then adjust for it
        yBallSt = max(0, -y)
        yBallEn = max(0,min(self.ySz_, self.ySz_ - (y + self.ySz_ - ySz)))
        xBallSt = max(0, -x)
        xBallEn = max(0,min(self.xSz_, self.xSz_ - (x + self.xSz_ - xSz)))
        srcIm = np.zeros((ySz, xSz, 4), dtype=np.uint8)
        #srcIm[y:y+self.ySz_, x:x+self.xSz_,:] = self.data_.im[:] 
        yLen, xLen = yBallEn - yBallSt, xBallEn - xBallSt
        if yLen >0 and xLen > 0:
            yImSt, xImSt = max(0, y), max(0, x)
            srcIm[yImSt:yImSt+yLen, xImSt:xImSt+xLen,:] =\
                                 self.data_.im[yBallSt:yBallEn, xBallSt:xBallEn,:] 
        surface = cairo.ImageSurface.create_for_data(srcIm, 
                                cairo.FORMAT_ARGB32, xSz,ySz)
        cr.set_source_surface(surface)      
        cr.rectangle(x, y, self.xSz_, self.ySz_)
        cr.fill()
项目:pyphy-engine    作者:pulkitag    | 项目源码 | 文件源码
def imprint(self, cr, xSz, ySz):
        #Get the position on the cnavas.
        ySt, xSt  = self.pos_.y_asint() - self.imSt_.y_asint(), self.pos_.x_asint() - self.imSt_.x_asint()
        yEn, xEn  = ySt + self.ySz_, xSt + self.xSz_
        #Get the data that can be pasted
        y1, x1 = np.abs(min(0, ySt)), np.abs(min(0, xSt))   
        y2     = self.ySz_ - np.abs(min(0, ySz - yEn)) 
        x2     = self.xSz_ - np.abs(min(0, xSz - xEn))
        #Correct for positions on canvas
        ySt, xSt = max(0, ySt), max(0, xSt)
        yEn, xEn = min(ySz, yEn), min(xSz, xEn)

        srcIm = np.zeros((ySz, xSz, 4), dtype=np.uint8)
        srcIm[ySt:yEn, xSt:xEn,:] = self.data_.im[y1:y2, x1:x2] 
        surface = cairo.ImageSurface.create_for_data(srcIm, 
                                cairo.FORMAT_ARGB32, xSz,ySz)
        cr.set_source_surface(surface)      
        cr.rectangle(xSt, ySt, x2 - x1, y2 - y1)
        cr.fill()
项目:pyphy-engine    作者:pulkitag    | 项目源码 | 文件源码
def generate_image(self, returnContext=False, 
                                cropObject=None, cropSz=None):
        '''
            returnContext: returns object of type cairo.Context
            cropObject   : the object around which image needs to be cropped
            cropSz       : the size of the image crop
        '''
        data    = np.zeros((self.ySz_, self.xSz_, 4), dtype=np.uint8)
        data[:] = self.baseCanvas_.im[:]
        surface = cairo.ImageSurface.create_for_data(data, 
                                cairo.FORMAT_ARGB32, self.xSz_, self.ySz_)
        cr      = cairo.Context(surface)
        for key, obj in self.objects_.iteritems():
            obj.imprint(cr, self.xSz_, self.ySz_)
        if returnContext:
            return data, cr
        else:
            return data
项目:my-weather-indicator    作者:atareao    | 项目源码 | 文件源码
def get_surface_from_pixbuf(pixbuf):
    surface = cairo.ImageSurface(
        cairo.FORMAT_ARGB32, pixbuf.get_width(), pixbuf.get_height())
    micairo = cairo.Context(surface)
    micairo.save()
    Gdk.cairo_set_source_pixbuf(micairo, pixbuf, 0, 0)
    micairo.paint()
    micairo.restore()
    return surface
项目:my-weather-indicator    作者:atareao    | 项目源码 | 文件源码
def get_surface_from_file(filename):
    if os.path.exists(filename):
        pixbuf = GdkPixbuf.Pixbuf.new_from_file(filename)
        if pixbuf:
            surface = cairo.ImageSurface(
                cairo.FORMAT_ARGB32, pixbuf.get_width(), pixbuf.get_height())
            context = cairo.Context(surface)
            Gdk.cairo_set_source_pixbuf(context, pixbuf, 0, 0)
            context.paint()
            return surface
    return None
项目:nvidia-multiple-smi    作者:ClementPinard    | 项目源码 | 文件源码
def draw_icon(machine,color1,color2):
    '''Draws a graph with 2 columns 1 for each percentage (1 is full, 0 is empty)'''
    WIDTH, HEIGHT = 22, 22
    if machine['nGPUs'] > 2:
        WIDTH = 11*machine['nGPUs'] #if more than 1 GPU on a machine, each column is 11px wide (and not 22px)
    surface = cairo.ImageSurface (cairo.FORMAT_ARGB32, WIDTH, HEIGHT)
    ctx = cairo.Context (surface)
    ctx.scale (WIDTH/machine['nGPUs'], HEIGHT) # Normalizing the canvas coordinates go from (0,0) to (nGPUs,1)

    for i in range(machine['nGPUs']):
        gpu = machine['GPUs'][i]
        percentage1,percentage2 = gpu['utilization']/100,gpu['used_mem']/gpu['memory']
        ctx.rectangle (i, 1-percentage1, 0.5, percentage1) # Rectangle(x0, y0, x1, y1)
        ctx.set_source_rgb(color1[0]/255,color1[1]/255,color1[2]/255)
        ctx.fill ()

        ctx.rectangle (i+0.5, 1-percentage2, 0.5, percentage2) # Rectangle(x0, y0, x1, y1)
        ctx.set_source_rgb(color2[0]/255,color2[1]/255,color2[2]/255)
        ctx.fill ()
    if 'i' not in machine.keys():
        machine['i'] = 0
    png_name = os.path.join(config_folder,machine['name']+str(machine['i'])+'.png')
    machine['i'] = (machine['i']+1)%2

    surface.write_to_png (png_name) # Output to PNG


    return(png_name)
项目:Triplet_Loss_SBIR    作者:TuBui    | 项目源码 | 文件源码
def get_numpy_array(self, size=256, scale=0.333):
        """
        using rsvg library, render the svg into a cairo surface with memory
        defined by a numpy array, effectively rendering the svg to an array
        """
        if self.tree_root is None:
          self.tree_root = self.tree_org.getroot()

        # get the result into a svg object (using rsvg lib)
        svg_object = rsvg.Handle(ET.tostring(self.tree_root))

        # create a numpy array to use as our canvas
        data = np.zeros((size, size, 4), dtype=np.uint8)
        surface = cairo.ImageSurface.create_for_data(
            data, cairo.FORMAT_ARGB32, size, size)

        cr = cairo.Context(surface)

        # fill with solid white and set scale (magic scale number)
        cr.set_source_rgb(1.0, 1.0, 1.0)
        cr.paint()
        cr.scale(scale, scale)

        # render our manipulated svg into cairo surface
        svg_object.render_cairo(cr)

        return data
项目:pyphy-engine    作者:pulkitag    | 项目源码 | 文件源码
def get_ball_im(radius=40, fColor=Color(0.0, 0.0, 1.0), sThick=2, sColor=None):
    '''
        fColor: fill color
        sColor: stroke color
        sThick: stroke thickness
    '''
    sz      = 2*(radius + sThick)
    data    = np.zeros((sz, sz, 4), dtype=np.uint8)
    surface = cairo.ImageSurface.create_for_data(data, 
                            cairo.FORMAT_ARGB32, sz, sz)
    cr      = cairo.Context(surface)
    #Create a transparent source
    cr.set_source_rgba(1.0, 1.0, 1.0, 0.0)
    cr.paint()
    #Create the border
    cx, cy = radius + sThick, radius + sThick
    cr.arc(cx, cy, radius, 0, 2*math.pi)
    cr.set_line_width(sThick)
    if sColor is not None:
        cr.set_source_rgba(sColor.b, sColor.g, sColor.r, sColor.a) 
    else:
        cr.set_source_rgba(0.0, 0.0, 0.0, 1.0) 
    cr.stroke()
    #Fill in the desired color
    cr.set_source_rgba(fColor.b, fColor.g, fColor.r, fColor.a)
    cr.arc(cx, cy, radius, 0, 2*math.pi)
    cr.fill()
    #cr.destroy()
    return cr, data
项目:pyphy-engine    作者:pulkitag    | 项目源码 | 文件源码
def get_rectangle_im(sz=gm.Point(4,100), fColor=Color(1.0, 0.0, 0.0)):
    data    = np.zeros((sz.y_asint(), sz.x_asint(), 4), dtype=np.uint8)
    surface = cairo.ImageSurface.create_for_data(data, 
                            cairo.FORMAT_ARGB32, sz.x_asint(), sz.y_asint())
    cr      = cairo.Context(surface)
    #Create a transparent source
    cr.set_source_rgba(1.0, 1.0, 1.0, 0.0)
    cr.paint()
    # Make rectangle and fill in the desired color
    cr.set_source_rgba(fColor.b, fColor.g, fColor.r, fColor.a)
    cr.rectangle(0, 0, sz.x_asint(), sz.y_asint())
    cr.fill()
    #cr.destroy()
    return cr, data
项目:pyphy-engine    作者:pulkitag    | 项目源码 | 文件源码
def get_arrow_im(pt, fColor=Color(0.0, 0.0, 0.0), arrowWidth=3.0):
    x, y = pt.x(), pt.y()
    sz   = int(np.ceil(max(abs(x), abs(y))))
    data = np.zeros((sz, sz, 4), dtype=np.uint8)
    surface = cairo.ImageSurface.create_for_data(data, 
                            cairo.FORMAT_ARGB32, sz, sz)
    cr      = cairo.Context(surface)
    #Create a transparent source
    cr.set_source_rgba(1.0, 1.0, 1.0, 0.0)
    cr.paint()
    #Start making the arrow
    cr.set_source_rgba(fColor.b, fColor.g, fColor.r, fColor.a)
    if x>=0 and y>=0:
        xSt, ySt = 0, 0
    elif x>0 and y < 0:
        xSt, ySt = 0, sz
    elif x <0 and y<0:
        xSt, ySt = sz, sz
    else:
        xSt, ySt = sz, 0
    stPoint = gm.Point(xSt, ySt) 
    cr.move_to(xSt, ySt)
    pt = pt + stPoint
    dirVec = pt - stPoint
    mag    = dirVec.mag()
    cr.line_to(pt.x(), pt.y())
    cr.set_line_width(arrowWidth)
    side1 = dirVec.rotate_point(-150)
    side1.scale(0.2)
    ang1  = pt + side1  
    cr.line_to(ang1.x(), ang1.y())
    side2 = dirVec.rotate_point(150)
    side2.scale(0.2)
    ang2  = pt + side2
    cr.move_to(pt.x(), pt.y())
    cr.line_to(ang2.x(), ang2.y())
    cr.stroke()
    return cr, data, stPoint
项目:pyphy-engine    作者:pulkitag    | 项目源码 | 文件源码
def paste_ball():
    bDef  = pm.BallDef()
    ball1 = pm.Ball.from_def(bDef, 'ball1', pm.Point(70,50))

    #Canvas Sz
    xCsz, yCsz = 640, 480
    #Create the base to paste it on
    data    = np.zeros((yCsz, xCsz, 4), dtype=np.uint8)
    surface = cairo.ImageSurface.create_for_data(data, 
                            cairo.FORMAT_ARGB32, xCsz, yCsz)
    cr      = cairo.Context(surface)
    cr.set_source_rgba(0.5, 0.5, 1.0, 1.0)
    cr.paint()

    #Create imSurface
    shp = ball1.data_.im.shape
    y, x  = ball1.pos_.y() - ball1.yOff_, ball1.pos_.x() - ball1.xOff_
    imDat    = np.zeros((yCsz, xCsz, 4), dtype=np.uint8)
    imDat[y:y+shp[0],x:x+shp[0],:] = ball1.data_.im[:] 
    surface = cairo.ImageSurface.create_for_data(imDat, 
                            cairo.FORMAT_ARGB32, xCsz, yCsz)
    cr.set_source_surface(surface)      
    #cr.set_source_rgb(1.0,0.0,0.0)     
    cr.rectangle(x, y, shp[0], shp[1])
    cr.fill()
    print y,x,shp[0],shp[1]
    return data
项目:neferset    作者:andburn    | 项目源码 | 文件源码
def setup_context(width, height, out_width=0):
    scale = 1
    if out_width >= MIN_WIDTH:
        scale = out_width / width
    surface = cairo.ImageSurface(
        cairo.FORMAT_ARGB32, int(round(width * scale)), int(round(height * scale)))
    ctx = cairo.Context(surface)
    ctx.scale(scale, scale)
    ctx.set_source_rgba(0, 0, 0, 0) # transparent bg
    ctx.paint()
    return (ctx, surface)
项目:pyphy-engine    作者:pulkitag    | 项目源码 | 文件源码
def get_block_im(blockDir, fColor=Color(1.0, 0.0, 0.0), 
                                    sThick=2, bThick=30, sColor=None):
    '''
        blockDir: the the direction in which block needs to be created
    '''
    stPoint = gm.Point(0,0)
    enPoint = stPoint + blockDir
    pts = get_box_coords(stPoint, enPoint, wThick=bThick)
    pt1, pt2, pt3, pt4 = pts
    #Create the points for drawing the block.
    mnX   = min(pt1.x(), pt2.x(), pt3.x(), pt4.x())
    mnY   = min(pt1.y(), pt2.y(), pt3.y(), pt4.y())
    mnPt  = gm.Point(mnX, mnY)
    pt1, pt2  = pt1 - mnPt, pt2 - mnPt
    pt3, pt4  = pt3 - mnPt, pt4 - mnPt  
    #print pt1, pt2, pt3, pt4

    if sColor is None:
        sColor = fColor
    xSz   = int(np.ceil(max(pt1.x(), pt2.x(), pt3.x(), pt4.x())))
    ySz   = int(np.ceil(max(pt1.y(), pt2.y(), pt3.y(), pt4.y())))
    data    = np.zeros((ySz, xSz, 4), dtype=np.uint8)
    surface = cairo.ImageSurface.create_for_data(data, 
                            cairo.FORMAT_ARGB32, xSz, ySz)
    cr      = cairo.Context(surface)
    #Create a transparent source
    cr.set_source_rgba(1.0, 1.0, 1.0, 0.0)
    cr.paint()
    #Create the border/Mask
    cr.move_to(pt1.x(), pt1.y())
    cr.line_to(pt2.x(), pt2.y())
    cr.line_to(pt3.x(), pt3.y())
    cr.line_to(pt4.x(), pt4.y())
    cr.line_to(pt1.x(), pt1.y())
    cr.set_line_width(sThick)
    cr.set_source_rgba(sColor.b, sColor.g, sColor.r, sColor.a)
    cr.stroke()
    #Fill in the desired color
    cr.set_source_rgba(fColor.b, fColor.g, fColor.r, fColor.a)
    cr.move_to(pt1.x(), pt1.y())
    cr.line_to(pt2.x(), pt2.y())
    cr.line_to(pt3.x(), pt3.y())
    cr.line_to(pt4.x(), pt4.y())
    cr.line_to(pt1.x(), pt1.y())
    cr.fill()
    return cr, data
项目:x-mario-center    作者:fossasia    | 项目源码 | 文件源码
def create_normal_surfaces(self,
                    context, vis_width, vis_height, star_width):

        rgba1 = context.get_border_color(Gtk.StateFlags.NORMAL)
        rgba0 = context.get_color(Gtk.StateFlags.ACTIVE)

        lin = cairo.LinearGradient(0, 0, 0, vis_height)
        lin.add_color_stop_rgb(0, rgba0.red, rgba0.green, rgba0.blue)
        lin.add_color_stop_rgb(1, rgba1.red, rgba1.green, rgba1.blue)

        # paint full
        full_surf = cairo.ImageSurface(
                        cairo.FORMAT_ARGB32, vis_width, vis_height)

        cr = cairo.Context(full_surf)
        cr.set_source(lin)
        cr.set_line_width(1)
        if self.rounded:
            cr.set_line_join(cairo.LINE_CAP_ROUND)

        for i in range(self.n_stars):
            x = 1 + i * (star_width + self.spacing)
            self.layout(cr, x + 1, 1, star_width - 2, vis_height - 2)
            cr.stroke_preserve()
            cr.fill()

        del cr

        # paint empty
        empty_surf = cairo.ImageSurface(
                        cairo.FORMAT_ARGB32, vis_width, vis_height)

        cr = cairo.Context(empty_surf)
        cr.set_source(lin)
        cr.set_line_width(1)
        if self.rounded:
            cr.set_line_join(cairo.LINE_CAP_ROUND)

        for i in range(self.n_stars):
            x = 1 + i * (star_width + self.spacing)
            self.layout(cr, x + 1, 1, star_width - 2, vis_height - 2)
            cr.stroke()

        del cr

        return full_surf, empty_surf
项目:x-mario-center    作者:fossasia    | 项目源码 | 文件源码
def create_normal_surfaces(self,
                    context, vis_width, vis_height, star_width):

        rgba1 = context.get_border_color(Gtk.StateFlags.NORMAL)
        rgba0 = context.get_color(Gtk.StateFlags.ACTIVE)

        lin = cairo.LinearGradient(0, 0, 0, vis_height)
        lin.add_color_stop_rgb(0, rgba0.red, rgba0.green, rgba0.blue)
        lin.add_color_stop_rgb(1, rgba1.red, rgba1.green, rgba1.blue)

        # paint full
        full_surf = cairo.ImageSurface(
                        cairo.FORMAT_ARGB32, vis_width, vis_height)

        cr = cairo.Context(full_surf)
        cr.set_source(lin)
        cr.set_line_width(1)
        if self.rounded:
            cr.set_line_join(cairo.LINE_CAP_ROUND)

        for i in range(self.n_stars):
            x = 1 + i * (star_width + self.spacing)
            self.layout(cr, x + 1, 1, star_width - 2, vis_height - 2)
            cr.stroke_preserve()
            cr.fill()

        del cr

        # paint empty
        empty_surf = cairo.ImageSurface(
                        cairo.FORMAT_ARGB32, vis_width, vis_height)

        cr = cairo.Context(empty_surf)
        cr.set_source(lin)
        cr.set_line_width(1)
        if self.rounded:
            cr.set_line_join(cairo.LINE_CAP_ROUND)

        for i in range(self.n_stars):
            x = 1 + i * (star_width + self.spacing)
            self.layout(cr, x + 1, 1, star_width - 2, vis_height - 2)
            cr.stroke()

        del cr

        return full_surf, empty_surf