Converts to RGBA and creates a 1-unit square for each pixel

This commit is contained in:
Ian Mackinnon
2011-04-26 17:48:37 +01:00
commit aa29c5ebc5
9 changed files with 9347 additions and 0 deletions

7
README.markdown Normal file
View File

@@ -0,0 +1,7 @@
# png2svg
Converts PNG images to SVG.
## Usage
png2svg input.png > output.svg

36
png2svg.py Executable file
View File

@@ -0,0 +1,36 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import Image
from StringIO import StringIO
def rgba_image_to_svg(im):
s = StringIO()
s.write("""<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg>\n""")
width, height = im.size
for x in range(width):
for y in range(height):
rgba = im.getpixel((x, y))
s.write(""" <rect x="%d" y="%d" width="1" height="1" style="fill:rgb%s; fill-opacity:%f; stroke:none;" />\n""" % (x, y, rgba[0:3], float(rgba[3])))
s.write("""</svg>\n""")
return s.getvalue()
def png_to_svg(filename):
try:
im = Image.open(filename)
except IOError, e:
sys.stderr.write('%s: Could not open as image file\n' % filename)
sys.exit(1)
im_rgba = im.convert('RGBA')
print rgba_image_to_svg(im_rgba)
if __name__ == "__main__":
assert len(sys.argv) == 2, "Usage: %s [FILE]"
png_to_svg(sys.argv[1])

76
test/generation/test.svg Normal file
View File

@@ -0,0 +1,76 @@
<?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="64"
height="48"
id="svg2"
version="1.1"
inkscape:version="0.48.0 r9654"
sodipodi:docname="test.svg"
inkscape:export-filename="/home/ian/jobs/png2svg/test/test.png"
inkscape:export-xdpi="90"
inkscape:export-ydpi="90">
<defs
id="defs4" />
<sodipodi:namedview
id="base"
pagecolor="#ffffff"
bordercolor="#666666"
borderopacity="1.0"
inkscape:pageopacity="0.0"
inkscape:pageshadow="2"
inkscape:zoom="10.6875"
inkscape:cx="32"
inkscape:cy="24"
inkscape:document-units="px"
inkscape:current-layer="layer1"
showgrid="false"
inkscape:window-width="808"
inkscape:window-height="824"
inkscape:window-x="975"
inkscape:window-y="180"
inkscape:window-maximized="0" />
<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"
transform="translate(0,-1004.3622)">
<path
sodipodi:type="arc"
style="fill:#00d455;fill-opacity:1;stroke:#0000ff;stroke-width:2.14815535;stroke-miterlimit:4;stroke-dasharray:none"
id="path3030"
sodipodi:cx="22.081871"
sodipodi:cy="17.964912"
sodipodi:rx="11.040936"
sodipodi:ry="10.573099"
d="m 33.122808,17.964912 a 11.040936,10.573099 0 1 1 -22.081873,0 11.040936,10.573099 0 1 1 22.081873,0 z"
transform="matrix(1.2320419,0,0,1.5830173,-8.539125,997.35032)" />
<rect
style="fill:#aa0000;fill-opacity:0.50000000000000000;stroke:#000000;stroke-width:3;stroke-miterlimit:4;stroke-dasharray:none"
id="rect3032"
width="34.678898"
height="29.719835"
x="23.596224"
y="1017.5724" />
</g>
</svg>

After

Width:  |  Height:  |  Size: 2.4 KiB

BIN
test/input/gray.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 642 B

BIN
test/input/mono.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 451 B

BIN
test/input/rgba.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

3076
test/output/gray.svg Normal file

File diff suppressed because it is too large Load Diff

After

Width:  |  Height:  |  Size: 338 KiB

3076
test/output/mono.svg Normal file

File diff suppressed because it is too large Load Diff

After

Width:  |  Height:  |  Size: 333 KiB

3076
test/output/rgba.svg Normal file

File diff suppressed because it is too large Load Diff

After

Width:  |  Height:  |  Size: 334 KiB