Polygonize Images and Other Vector Graphics Fun in Tableau

Last June, Nicco Cirone wrote a brilliant blog post for the Information Lab called Polygons to the people! Polygonize any imagefor Tableau, in which he detailed a process he had developed for converting images into polygons—which can be easily visualized in Tableau—by first converting them to Scalable Vector Graphics (SVG). Using this method, he was able to transform an image into the following Tableau visualization, which shows chicken consumption by continent:




The rest of this post will build upon Nicco’s approach, so if you haven’t read it yet, give it a quick read and then come back.

Converting Images to Polygons
So, when I had a recent need to parse some SVG images for my post on creating Geometric Art in Tableau, I immediately revisited Nicco’s tutorial to see if I could borrow from it. Unfortunately, I ran into a couple of problems. First, the SVG images produced by Inkscape included only polygon path objects (essentially a list of points which form a polygon), but the images I needed to parse had a variety of objects, including ellipses, rectangles, and triangles, in addition to polygons. Second, I don’t have access to Alteryx, which he used to parse the SVG into polygon files. In the end, I wrote a Python program to perform the parsing and it worked quite well.

But after completing that project, I found myself returning to Nicco’s post and wanting to try out his process. The idea of being able to convert virtually any image to polygons usable by Tableau was just too exciting. But I still had the issue of not having an Alteryx license. Coincidentally, Nicco’s post originated because of a similar issue. He had originally hoped to use a technique developed by his colleague, Brian Prestidge. But Brian’s method required a license of Adobe Illustrator, which Nicco didn’t have. So, here I was, in a similar boat as Nicco…wanting to follow the procedure, but lacking the necessary software to do so.

After thinking about this for a bit, I decided to try to modify my geometric art Python code so that it could handle the SVG images created by Inkscape. And, by doing this, I’d be able to provide a fully open source option for polygonizing images. So, with some relatively simple modifications, I was able to do just that. I then created this polygon-based visualization of Pangea.



And, of course, I just couldn’t resist re-creating Nicco’s chicken viz (with his permission, of course).



SVGs Are Cool!
SVG files are really quite amazing as they contain all details about the image, in readable text. But you don’t need to generate the SVG files from some other tool such as Geometrize or Inkscape in order to convert them to Tableau polygons. Many images are already available in SVG format and most of the top image sites include SVG versions of many of their images. So, if you can find an image already in SVG format, then it’s possible for you to use similar methods to parse that SVG into polygons which can be visualized in Tableau. To demonstrate this, I found a royalty-free SVG of the inimitable Betty White on www.vecteezy.com. The SVG was relatively simple, containing some basic polygon shapes. With some slight modifications to my code, I was able to parse out the shapes and write them to a polygon file, which I then visualized in Tableau.



Of course, the SVGs we’ve dealt with here are fairly simple, containing only a handful of different object types, so they are relatively easy to parse. More complex images would be significantly more difficult, but with enough patience, you could polygonize anything you desire.

How-To
Okay, so here’s how you can use this method yourself.

1) Leverage Nicco’s instructions through the step where he saves the vector image as an SVG file.
2) If you’re familiar with Python, you can download the code for Polygon SVG.py.
3) If not, you can download the executable package, PolygonSVGPackage.zip. Extract all the files to your computer, then run PolygonSVG.exe. Sorry Mac users, but I think this will only work for Windows, but if you really want to try this, I’m happy to help you get Python set up on your machine. Fortunately, you don’t have to know much about Python to run the code.
4) The program will guide you through the process of selecting a source SVG and will then output two csv files, one with color information and another with polygons.
5) Based on the color csv, create your custom color palette, if needed (I didn’t need color for my visualizations as I ended up recoloring them, so this step may not be necessary).
6) Connect to the polygon csv in Tableau.
7) Drag X to the Rows shelf and Y to the Columns shelf.
8) Reverse the y axis.
9) Drag Polygon ID to the Color card, then choose your new custom color palette (if desired).
10) Change the chart to Polygon.
11) Drag Point ID to the Path card.
12) Make other adjustments as desired including gridlines, fixed axes, etc.

Ken Flerlage, January 27, 2018

13 comments:

  1. Hey Ken - great post. Really helpful. Exactly what I was hoping to learn to do. For the zip file, when I noticed there is only a file called PolygonSVG.exe. Should there be a separate file for ParseSVG.exe? When I use the PolygonSVG file, the polygon CSV file is empty. Wasn't sure if I was doing something wrong.
    Thank you again.

    ReplyDelete
    Replies
    1. PolygonSVG.exe should be the only one. Not exactly sure what could be happening.

      Delete
  2. awesome stuff, but I'm getting a failure on an SVG with curves, etc. Should the code work on paths as well as polygons, or am I screwing up?

    Thanks!

    ReplyDelete
    Replies
    1. This specific code is intended to work with SVG path objects which form polygons. Did you generate your SVG using the Inkscape method Nicco developed?

      Delete
    2. In my excitement I skipped right to "No need to have alteryx" and missed the inkscape transformation. I'm guessing my SVG is via Illustrator. Let's give this another shot...

      Thanks!

      Delete
    3. Revisiting this after a bit. I've been able to get polygon only svg for the input.

      I'm seeing a few polygons where the x and the y coordinate are the same (mostly low values), causing a spike or spikes to appear in the middle of what is looking like the right output.

      Filtering and removing these is helping, but the scaling looks off relative to polygons that didn't have these tiny points.

      Certainly not looking to complicate life; at this point I'm mostly curious if I'm alone in having this issue.

      [Has anyone asked tableau to make this a native feature?]

      Delete
    4. This process can be a bit touchy, but I don't recall experiencing similar issues.

      Delete
  3. Circling back after a few months of head scratching, and ... Eureka!

    I had been using an Illustrator generated SVG file with both paths and polygons.

    Adding a copy of the main loop allowed for the polygons to be detected.

    I still generated artifacts. These came in three varieties:
    1. A point was duplicated in a polygon
    2. A 'random' point near the origin of the form (X,Y=X) was introduced to a polygon
    3. A small polygon was included in the list of shapes generated.

    Deleting these and reindexing the PointIDs rendered a perfect map.

    Thanks for all your help!
    John

    ReplyDelete
  4. Thanks for this awesome great source of knowledge here

    ReplyDelete
  5. I'm running into the same issue other users had - my exported CSV's are blank. I've tried with an SVG exported by both Illustrator and Inkscape... any ideas?

    ReplyDelete
    Replies
    1. Not sure. Would you be able to email me with further details? flerlagekr@gmail.com

      Delete
  6. Hi Ken,

    Thanks for this amazing blog. While I have my hands-on to do this, I got stuck with the output files provided by PolygonSVGPackage.zip. It gave me two empty files(1kb) for my svg file. I also tried my best to check with other SVG files available online and they too gave me empty files. Is the code not working or do I need to double check my sys configuration or something. Please help me out on this.

    Thank you for inspiring us.

    ReplyDelete
    Replies
    1. Hmm...would you be able to send me an email and include the svg files? flerlagekr@gmail.com

      Delete

Powered by Blogger.