Skip to content

Instantly share code, notes, and snippets.

@fernandoc1
Created July 6, 2016 14:00
Show Gist options
  • Save fernandoc1/40a8f88236e6d4dbaf498592c66e996e to your computer and use it in GitHub Desktop.
Save fernandoc1/40a8f88236e6d4dbaf498592c66e996e to your computer and use it in GitHub Desktop.
Creating colorful Google Markers with python for using in CGI.
#! /usr/bin/python
from xml.dom import minidom
import os
import sys
import cairo
import rsvg
markerDocument = minidom.parse('marker.svg')
svg = markerDocument.getElementsByTagName("svg")[0]
imgHeight = float(svg.attributes['height'].value)
imgWidth = float(svg.attributes['width'].value)
color = os.environ['QUERY_STRING']
if (len(color) != 6):
color = "00FF00"
style = "clip-rule:evenodd;fill-rule:evenodd;stroke:#000000;stroke-width:0.58006144;stroke-miterlimit:10;fill-opacity:1;"
style += "fill:#"+color
markerPath = markerDocument.getElementsByTagName("path")[0]
markerPath.setAttribute("style", style)
svgXmlData = markerDocument.toxml()
img = cairo.ImageSurface(cairo.FORMAT_ARGB32, int(imgWidth), int(imgHeight))
ctx = cairo.Context(img)
handle = rsvg.Handle(None, svgXmlData)
handle.render_cairo(ctx)
print "Content-type: image/png\n"
img.write_to_png(sys.stdout)
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment