Search This Blog

Tuesday, October 9, 2018

Powershell SVG Maker

I was going to sit around ill at hone and make an SVG one little square at a time in Inkscape of this image I use...



However, after 4 dots I got bored.

I know that an SVG file is an XML based descriptor of images and shapes and know that I can use  minimal information to make a test file that is similar to the following 2x2 block...

<xml version="1.0" encoding="UTF-8"?>
<svg version="1.1" baseProfile="full" width="20" height="20" xmlns="http://www.w3.org/2000/svg">
<rect fill="#000000" x="0" y="0" width="10" height="10"/>
<rect fill="#000000" x="10" y="0" width="10" height="10"/>
<rect fill="#ffffff" x="0" y="10" width="10" height="10"/>
<rect fill="#000000" x="10" y="10" width="10" height="10"/>
</svg
>

So... by grabbing the image, and looping through the pixels we can create all the rects we need. we can even keep the original color of the pixels. This routine also has a scaling factor in it.


$FilePath = 'LesleyatHonda.12.Dither.Part.White.png' $Image = [System.Drawing.Image]::FromFile($filePath) # Simple dirty UTF-8 outfile << THIS IS MPORTANT!! $OutFilePath = 'LesleyatHonda.svg' $SVGScale = 10 '<?xml version="1.0" encoding="UTF-8"?>' | Out-File $OutFilePath -Encoding utf8 '<svg version="1.1" baseProfile="full" width="'+($Image.Width*$SVGScale)+'" height="'+($Image.Width*$SVGScale)+'" xmlns="http://www.w3.org/2000/svg">' | Out-File $OutFilePath -Append -Encoding utf8 For($ypos=0;$ypos -lt $Image.Height;$ypos++){ For($xpos=0;$xpos -lt $Image.Width;$xpos++){ $Pixel = $Image.GetPixel($xpos,$ypos) '<rect fill="#'+($Pixel.Name.substring(2,6))+'" x="'+($xpos*$SVGScale)+'" y="'+($ypos*$SVGScale)+'" width="'+$SVGScale+'" height="'+$SVGScale+'"/>' | Out-File $OutFilePath -Append -Encoding utf8 } }; '</svg>' | Out-File $OutFilePath -Append -Encoding utf8