User:Dave Young/Computata Miscellanea: txt-to-svg.py

From XPUB & Lens-Based wiki


txt-to-svg.py

This script is a development of previous experiments in prototyping sessions and also the last OSP workshop with using python to generate Literal Draw code. In this version it bypasses the Literal Draw application in favour of directly producing an svg file that is viewable in a browser or can be edited in Inkscape without any intermediary steps. To draw with the script, you make a .txt file and draw uppercase 'X' characters where you want a square pixel to be drawn, any other character denotes a blank space. It is possible to modify the file and add more shapes if you wish - look for the cmd variable and put in your own svg code to be iterated instead.

Here is a naff logo built with txt-to-svg and inspired by MicroProse logo from the 80s/90s (they made some pretty serious pc games such as X-Com and Pizza Tycoon). Txttosvg.svg

And here is the txt file that generated it. Please use the code and do something more interesting with it.

-------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------
--------------------------------------------XXXXXXXXXXXXX----------------------------------------------
----XXXXXXXXXXXXXXX-----------------------XXXXXXXXXXXXXXXX---------------------------------------------
---XXXXXXXXXXXXXXX-----------------------XXXX----------XXX---------------------------------------------
---------XXX----------------------------XXXX----------XXX----------------------------------------------
---------XXX----------------------------------------XXXX-----------XXXXXX------------------------------
--------XXX----XXXXXXXXXX-----XXXXXXXXXXXXXXXX-----XXXX-----------XXXXXXXXX----------------------------
--------XXX---XXXXXXXX-XXX---XXXXXXXXXXXXXXXX----XXXXX-----------XXX----XXX-XXXXXXXXXXXXXXXXXXXXXXXX---
-------XXX----XXX-------XXX-XXX-----XXX--------XXXXXX-----------XXX------------------------------------
-------XXX---XXXXXX------XXXXX------XXX-------XXXXX-------------XXXX-------XXXX----XXXX--XXXXXXXX------
------XXX----XXX--------XXX-XXX----XXX-------XXXX----------------XXXXXXXX---XXX----XXX--XXXXXXXXXX-----
------XXX---XXXXXXXX---XXX---XXX---XXX----XXXXXX----------------------XXXX--XXX---XXX--XXX-------------
-----XXX----XXXXXXX---XXX-----XXX-XXX----XXXXXXXXXXXXXXXXXXXXX--------XXXX--XXX--XXX--XXX----XXXX------
-----XXX-----------------------XXXXXX--XXXXXXXXXXXXXXXXXXXXXX--XXX---XXXX---XXX-XXX--XXX------XX-------
---------------------------------------------------------------XXXXXXXXX----XXXXXX----XXXXXXXXX--------
----XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX--XXXXXX------XXXX-------XXXXXXX---------
-------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------


Code

The scripts takes three arguments: a textfile that functions as a grid-template for what is to be drawn, an outputfilename.svg, and the size of each "pixel" in the drawing.

#!/usr/bin/python
# automates grid-based transformations
# Args: txtfile.txt | outputsvg.svg | pixelsize
# h6!


import sys

header = """<svg xmlns="http://www.w3.org/2000/svg" version="1.1"><g>"""
footer = """</g></svg>"""

thegrid = open(sys.argv[1], "r") #grid template - X = draw commands
output = open(sys.argv[2], "w") #the outputted text to paste into 

x = 0
y = 0
sz = int(sys.argv[3])

output.write(header)

def commands(x, y, sz):
	# the cmd string is the svg tag to be iterated - if you want other shapes/parameters add them in here!
	cmd = """<rect style="fill:#000;stroke:none;" width="{2}" height="{2}" x="{0}" y="{1}" />\n""".format(x, y, sz)
	output.write(cmd)


for line in thegrid:
	ret=0
        # scan each character of the line horizontally
	for char in line:
		x+=sz    # incremenent x by the pixel size variable
                
		if char == str('X'):    # if an X is found, print commands to file
			commands(x, y, sz)
	x=0    # reset x to the beginning of a new line
	y+=sz  # increment y - new row
# write tags to close svg
output.write(footer)

# close files
thegrid.close()
output.close()