User:Laurier Rochon/prototyping/flag generator: Difference between revisions

From XPUB & Lens-Based wiki
(Created page with "So you instantiate this class, and it creates an SVG flag! (assuming the dir you are in is writable...) <source lang="python"> from vars import * import random class Flag: de...")
 
No edit summary
 
(3 intermediate revisions by the same user not shown)
Line 1: Line 1:
So you instantiate this class, and it creates an SVG flag! (assuming the dir you are in is writable...)
So you instantiate this class, and it creates an SVG flag! (assuming the dir you are in is writable...)
[[File:Flag1.svg]]
[[File:Flag2.svg]]


<source lang="python">
<source lang="python">
Line 96: Line 99:
end = '''</g></svg>'''
end = '''</g></svg>'''


</python>
</source>
 
 
[[File:Flag3.svg]]
[[File:Flag4.svg]]
[[File:Flag5.svg]]
[[File:Flag6.svg]]

Latest revision as of 16:44, 13 October 2011

So you instantiate this class, and it creates an SVG flag! (assuming the dir you are in is writable...)

Flag1.svg Flag2.svg

from vars import *
import random

class Flag:

	def __init__(self):
		self.canvasx=20
		self.canvasy=20
		self.stripes=3
		self.orientation=random.randint(0,1)
		if(self.orientation):
			self.flag_height = 250
			self.twidth=390
			self.theight=round(self.flag_height/self.stripes)
		else:
			self.flag_width = 350
			self.twidth=round(self.flag_width/self.stripes)
			self.theight=200

	def makestripe(self,no):
		hexcode = random.randint(0, 16777215)
		style = "fill:#%x;fill-rule:evenodd;stroke:none" % hexcode
		shapeid = hexcode
		if(self.orientation):
			myx = self.canvasx
			myy = no*(self.theight+1)+self.canvasy			
		else:
			myy = self.canvasy
			myx = no*(self.twidth+1)+self.canvasx

		stripecode = "<rect style='%s' id='%s' width='%s' height='%s' x='%s' y='%s' />" % (style,hexcode,self.twidth,self.theight,myx,myy)
		return stripecode
	
	def makeflag(self,stripes):
		rects_code = ""
		for a in range(0,stripes):
			rects_code = rects_code + self.makestripe(a) + '\n'
		return rects_code;

	def assemble(self):
		all = start + self.makeflag(self.stripes) + end
		return all
	
	def write_svg(self):
		f = open('flag.svg', 'w')
		f.write(self.assemble().strip(' \t\n\r'))
		f.close()

x = Flag()
x.write_svg()

And the imported file. Needed for import to work.

start = '''<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!-- Created with Inkscape (http://www.inkscape.org/) -->

<svg
   xmlns:dc="http://purl.org/dc/elements/1.1/"
   xmlns:cc="http://creativecommons.org/ns#"
   xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
   xmlns:svg="http://www.w3.org/2000/svg"
   xmlns="http://www.w3.org/2000/svg"
   xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
   xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
   width="400"
   height="300"
   id="svg2"
   version="1.1"
   inkscape:version="0.48.1 r9760"
   sodipodi:docname="New document 1">
  <defs
     id="defs4" />
  <metadata
     id="metadata7">
    <rdf:RDF>
      <cc:Work
         rdf:about="">
        <dc:format>image/svg+xml</dc:format>
        <dc:type
           rdf:resource="http://purl.org/dc/dcmitype/StillImage" />
        <dc:title></dc:title>
      </cc:Work>
    </rdf:RDF>
  </metadata>
  <g
     inkscape:label="Layer 1"
     inkscape:groupmode="layer"
     id="layer1">

'''
end = '''</g></svg>'''


Flag3.svg Flag4.svg Flag5.svg Flag6.svg