Saturday, September 3, 2016

adobe illustrator - Script to get x,y positions from a path in a vector image


In the old days, if someone had a scientific graph, then readers would only have access to the rastered image. So if you wanted to get the raw data from the plot, you could use something like DataThief or Webplotdigitizer on a rastered image like the one below.


Raster image example


These days, scientific publications usually require authors to submit plots in vector format (pdf or eps). Which means you can directly open up the plot in illustrator and see the data being plotted (usually as some path object?). For example, Figure 5a from here is a vector plot that is accessible from the pdf (reproduced below).


From the referenced pdf Figure 5a


My questions are the following:



  1. What are the usual object types for a line in an XY plot (see Figure 5a in the above pdf)?


  2. What is the best way to run a script over the object (in Illustrator or some other program) to get the exact position of the points of the line?


Edit: I already saw this question, but rather than just getting a single point associated with the object, I wanted to absolute position of all the sub-points (if that makes sense). That way I can recreate the graph using the positions of the axes labels.



Answer



The usual object type for a graph in vector graphic is a path (called pathItem in the Illustrator JavaScript API). It could be one joined ragged line, a smooth bezier curve and sometimes separate straight lines. Depends on the program generating the graph.


I have written a little script to get you started extracting the points from a graph:


#target illustrator

// the current selection
var selection = app.activeDocument.selection;


// create a txt-file for the data
var file = File.saveDialog('Save a comma-separated list of the x and y coordinates of the points of the current selection, relative to the coordinate system of the artboard.', 'Comma-separated values:*.csv');

// enable writing to the file
file.open('w')

// iterate through each selected item
for (var i = 0; i < selection.length; i++) {
var item = selection[i];


// check if selection is a PathItem
if (item.typename === "PathItem") {

// every PathItem has a list of pathPoints
var points = item.pathPoints;

// iterate through each pathPoint of the item
for (var j = 0; j < points.length; j++) {


// the point in document coordinates, relative to the center of the whole document
var documentPoint = points[j].anchor;

// the point converted to artboard coordinates, relative to where you have placed the origin of the coordinate system
var artboardPoint = app.activeDocument.convertCoordinate(documentPoint, app.coordinateSystem, CoordinateSystem.ARTBOARDCOORDINATESYSTEM);

// write the artboard coordinates to the file
file.write(artboardPoint[0] + "," + artboardPoint[1] + "\n");
}
}

}

// stop writing to the file and save it
file.close();

The script creates a .csv file containing a comma-separated list of the x and y coordinates of the points of the current selection, relative to the coordinate system of the artboard.


I would recommend first moving the origin of the artboard ruler to the origin of the graph, then select only the graph itself and run the script.


If your graph is made of separate lines, you will get a lot of duplicate points. You could remove them in the .csv file or you could "clean up" your graph first using my script from this answer or integrate the two scripts.


No comments:

Post a Comment

technique - How credible is wikipedia?

I understand that this question relates more to wikipedia than it does writing but... If I was going to use wikipedia for a source for a res...