X Y data from pdf with inkscape
The snippet can be accessed without any authentication.
Authored by
Lars Spannan
Use Inkscape and a Matlab/octave script to extract x-y plot data from vector figures in pdf files.
For a Jupyter notebook version with screenshots see https://github.com/larssp/x-y-data-from-pdf-with-inkscape
- Pick the positions (in pt) of minimum and maximum x and y ticks from the figure in Inkscape. Define them in the first part of the script.
- Click on respective line and press
Shift+Ctrl+X
to open the XML editor. Copy the SVG command string. - Check if created plot matches the figure from the pdf.
SVG2COO.m 12.01 KiB
%% Calibrate the axis
clear;
clc;
paige_height = 841.88977; % page height in pt (Shift+Ctrl+D)
x1_koo = 0; % first x coordinate (usually 0)
x1_unit = 335.160; % inkscape coordinate of the x-tick (in pt !!!)
y1_koo = 0; % first y coordinate (usually 0)
%y1_unit = 383.238; % Default Inkscape coodinate system starts from the top left corner
y1_unit = paige_height-383.238; % If the default coordinate system behaviour in the inkscape properties is changed,
% the y-coordinate has to be subtracted from the page height
% https://wiki.inkscape.org/wiki/index.php/Release_notes/1.0#Y-Axis_Inversion
x2_koo = 60; % second x coordinate (usually x_max)
x2_unit = 525.121; % inkscape coordinate of the x-tick (in mm/pt/etc.)
y2_koo = 1/1000; % second y coordinate (usually y_max)
%y2_unit = 535.102;
y2_unit = paige_height-535.102; % subtraction as described above, if default behaviour is changed
% curve translation (this additional svg command may be used in rare cases. Check the XML code)
% If the curve translation command is absent, both values are 0
t(1) = 0;
t(2) = 0;
h(1,1) = x1_unit;
h(1,2) = x1_koo;
h(2,1) = x2_unit;
h(2,2) = x2_koo;
v(1,1) = y1_unit;
v(1,2) = y1_koo;
v(2,1) = y2_unit;
v(2,2) = y2_koo;
%% Insert the Inkscape SVG command string
% click on line and press Shift+Ctrl+X to open the XML editor
"m 335.16016,458.76172 h 0.30078 l 0.66015,-0.35938 h 0.30079 l 0.30078,-0.12109 0.35937,-0.53907 0.30078,-0.30078 0.35938,0.17969 0.30078,-1.32031 0.29687,0.48047 0.24219,-1.32032 0.41797,0.35938 0.30078,-2.16016 0.24219,2.28125 0.41797,-4.74218 0.24219,1.20312 0.41796,2.27734 0.30079,-8.27734 0.23828,-2.88281 0.30078,5.10156 0.35937,8.76172 0.30078,-15.72266 0.66016,-29.39843 0.30078,-8.58204 0.23828,-2.45703 0.42188,3.0586 0.53906,12.96093 0.42188,6.41797 0.23828,0.66016 0.42187,-9.89844 0.29688,-4.19922 0.24218,17.75782 0.41797,-1.98047 0.24219,-20.69922 0.30078,33.35937 0.41797,-19.32031 0.24219,0.48047 0.29687,-0.83984 0.30078,14.03906 0.35938,-44.45703 0.30078,65.39843 0.35938,-87 0.30078,28.73829 0.30078,12.0625 0.35937,-60 0.30078,17.82031 0.30079,66.41797 0.53906,-124.98047 m 0.17969,0 0.60156,79.32031 0.29687,32.28125 0.24219,-14.76172 0.30078,-26.03906 0.35938,11.40234 0.30078,33.77735 0.41797,23.04297 0.24218,-6.72266 0.29688,-20.46094 0.35937,-1.3164 0.30079,9.77734 0.30078,14.33984 0.24218,7.5 0.41797,-14.57812 0.23828,-4.19922 0.42188,6.05859 0.30078,-8.51953 0.23828,6.23828 0.30078,-4.25781 0.35938,4.01953 0.30078,-1.92187 0.42187,0.96093 0.23829,-0.60156 0.30078,1.38281 0.35937,-3.54296 0.30078,10.26171 0.30078,-10.3789 0.29688,14.33984 0.35937,-0.35937 0.30079,-10.92188 0.35937,-1.5 0.30078,12.0586 0.30078,-17.87891 0.23828,-6.17969 0.66016,17.40235 0.42188,7.97656 0.30078,-14.33985 0.23828,-11.03906 0.42187,-5.51953 0.23828,-14.64062 0.30079,-7.37891 0.41796,3.12109 0.24219,-7.20312 0.30078,-9.0586 0.30078,3.66016 0.35938,-1.07812 0.29687,-4.6211 0.96094,37.6211 0.35938,24 0.30078,8.8164 0.66015,-36.71875 0.24219,-21.78125 0.29688,7.02344 0.42187,29.9375 0.23828,26.69922 0.30078,-27.47656 0.30079,12.83984 0.35937,2.27734 0.30078,0.78125 0.30078,2.75782 0.35938,-9.0586 0.30078,19.98047 0.35937,-14.10156 0.30079,12.96094 0.29687,-14.82032 0.36328,11.94141 0.29688,-10.74219 0.66015,8.16016 0.30078,-9.47656 0.24219,9.47656 0.29688,-1.79688 0.36328,-6.17968 0.29687,7.07812 0.30078,2.51953 0.35938,-6.89844 0.30078,0.83985 0.30078,6.48047 0.35938,1.4375 0.30078,-3.66016 0.35937,-0.95703 0.30078,1.25781 0.30079,1.74219 0.66015,2.63672 0.23828,0.12109 0.66016,-4.19922 0.30078,-0.60156 0.66016,2.94141 0.30078,0.78125 0.66015,-2.16016 h 0.29688 l 0.35937,2.09766 0.30079,2.82031 0.30078,-0.96094 0.35937,-2.9375 0.30078,-1.5 0.30078,0.66016 0.23829,1.61719 0.42187,0.96093 0.30078,0.24219 0.23828,0.11719 0.42188,-0.66016 0.23828,-1.3164 0.30078,0.53906 0.35938,2.09765 0.71875,-2.63671 0.24218,1.67968 0.30078,0.83985 0.35938,-1.67969 0.29687,1.01953 0.30079,0.29688 0.24218,-0.17969 0.41797,0.66015 0.30078,-1.67968 0.23828,2.82031 0.42188,-2.82031 0.23828,0.0625 0.30078,2.45703 0.42188,-4.25781 0.23828,5.03906 0.42187,-3.71875 0.23828,2.27734 0.30079,-0.35937 0.35937,0.66015 0.30078,-1.4375 0.30078,1.73828 0.30078,-2.8789 0.35938,3.05859 0.29687,-1.62109 0.36329,0.96094 0.29687,0.96093 0.30078,-2.10156 0.35938,0.83984 h 0.30078 l 0.30078,0.30079 h 0.35937 l 0.30079,2.03906 0.23828,-4.01953 0.36328,3.30078 0.29687,-0.60156 0.30078,0.24218 0.30079,-2.39843 0.35937,3.41796 0.30078,-1.26171 0.35938,0.42187 0.30078,-2.28125 0.30078,3.12109 0.35937,-0.78125 0.30079,1.14063 0.29687,-3.17969 0.36328,1.98047 0.29688,-0.42187 0.24218,1.4414 0.41797,-0.66015 0.30078,0.53906 0.24219,-0.59766 0.29688,-1.4414 0.36328,1.80078 0.29687,-1.44141 0.36328,1.5625 0.29688,-0.12109 0.30078,0.12109 0.35938,1.01953 0.30078,-0.90234 0.30078,1.5625 0.35937,-1.5625 0.30078,0.96094 0.30079,0.30078 0.35937,-1.26172 0.30078,1.74219 0.29688,-1.01954 h 0.24218 l 0.41797,1.19922 0.24219,-1.32031 0.30078,0.17969 0.41797,0.78125 0.24219,-1.26172 0.41797,-0.0586 0.24218,0.83984 0.29688,-1.19922 0.35937,0.59766 0.30079,1.08203 0.30078,-1.08203 0.35937,1.26172 0.30078,0.48046 0.30078,-1.32031 0.23829,-0.60156 0.42187,1.26172 0.23828,-0.48047 0.30078,0.0586 0.42188,0.96094 0.23828,-0.48047 0.42187,-1.14062 0.23829,0.78125 0.30078,1.5 0.66015,-1.98047 0.66016,1.62109 0.59766,-1.4414 0.30078,0.90234 0.35937,0.71875 0.30078,-1.01953 0.23828,-0.66016 0.42188,1.37891 0.30078,0.71875 0.35938,-1.61719 0.30078,-0.24219 0.23828,1.44141 0.72265,-0.83984 0.23829,0.12109 0.41796,-0.48047 0.54297,1.67969 0.30078,-1.5 0.65625,0.30078 0.30079,0.23828 0.35937,1.14063 0.30078,-1.6211 0.35938,0.1211 h 0.30078 l 0.24219,0.30078 0.41796,0.83984 0.30079,-0.96094 0.23828,0.48047 0.42187,-0.96094 0.23828,0.66016 0.30078,-0.23828 0.30079,0.89844 0.35937,-0.17969 0.30078,-0.53906 0.66016,-0.30078 0.30078,0.35937 0.66016,0.48047 0.29687,-0.30078 h 0.36328 l 0.59766,-0.53906 0.35937,0.48046 0.30078,-0.35937 0.24219,1.37891 0.29688,-0.96094 0.42187,0.96094 0.23828,-1.32032 0.42188,0.48047 0.23828,-0.35937 0.30078,-0.17969 0.42188,0.53906 0.23828,-0.53906 0.30078,0.89844 0.30078,-0.23828 0.35937,0.0586 0.30079,0.17969 0.35937,-0.66016 0.30078,0.66016 0.29688,-0.66016 0.24218,-0.23828 0.41797,0.71875 0.24219,-0.66016 0.41797,0.1211 0.24219,0.83984 0.30078,-0.78125 0.41797,0.30078 0.24218,0.83985 0.29688,-0.83985 0.30078,0.30078 0.35938,0.17969 0.30078,-0.66016 0.35937,0.35938 0.30078,0.48047 0.30078,-0.96094 h 0.30079 l 0.35937,0.66016 0.30078,-0.48047 0.35938,-0.41797 0.59765,0.83984 0.36328,-0.66015 h 0.29688 l 0.24219,0.53906 0.30078,0.12109 0.35937,-0.60156 h 0.30078 l 0.41797,0.60156 0.23828,0.0586 0.30079,-0.53907 0.30078,-0.12109 0.66015,1.07813 0.35938,-0.65625 0.30078,-0.30079 0.30078,0.17969 0.35938,0.66016 0.30078,-0.17969 0.23828,-0.60156 0.30078,-0.0586 0.35937,0.17969 0.30079,0.66016 0.41796,-0.17969 0.24219,-0.48047 0.30078,-0.17969 0.30078,0.0586 0.65625,0.78125 0.66016,-0.83985 0.30078,-0.12109 0.30078,0.17969 0.35938,0.48047 0.30078,0.23828 0.66016,-0.71875 0.30078,-0.17969 0.35937,0.12109 0.53906,0.77735 0.30079,0.17969 0.66015,-0.65625 0.41797,-0.1211 0.83984,0.35938 h 0.66016 l 0.24219,0.30078 0.41797,0.17969 1.20312,-1.01954 h 0.29688 l 0.36328,0.0586 0.66015,0.48047 0.29688,0.30078 0.30078,0.11719 0.66016,-0.29688 h 0.30078 l 0.23828,0.17969 h 0.42187 l 0.96094,-0.53906 0.53906,-0.24219 h 0.41797 l 0.90235,0.42187 h 0.30078 l 0.66015,-0.30078 h 0.95704 l 1.19921,0.35938 h 0.30079 l 0.66015,-0.23828 0.42188,-0.1211 H 472.5 l 0.96094,0.30078 h 0.66015 l 0.95703,-0.30078 h 0.60157 l 0.66015,0.17969 0.66016,0.41797 0.53906,0.17969 H 478.5 l 0.35937,-0.11719 0.30079,-0.30078 0.66015,-0.17969 h 0.30078 l 0.30078,-0.0586 h 0.35938 l 0.95703,0.23828 0.30078,0.23828 0.90235,0.24219 0.29687,-0.0625 0.36328,-0.29688 0.95703,-0.35937 h 0.30078 l 0.30079,0.0586 h 0.35937 l 0.30078,0.17969 0.66016,0.23828 h 0.30078 l 0.29687,-0.23828 h 0.90235 l 0.66015,0.23828 0.71875,0.24219 0.90235,-0.53907 0.29687,-0.12109 0.36328,0.12109 h 0.29688 l 0.30078,0.17969 h 0.35938 l 0.30078,-0.12109 0.30078,-0.17969 h 0.66015 l 0.96094,0.30078 h 0.23828 l 0.42188,-0.17969 h 0.53906 l 0.41797,0.0586 0.24219,0.23828 0.30078,0.0625 0.35937,-0.0625 0.30078,-0.23828 0.29688,-0.0586 0.66016,0.29688 0.30078,0.17969 h 0.35937 l 0.30078,-0.29688 0.30078,-0.12109 0.66016,0.23828 0.30078,0.24219 0.35938,-0.0625 L 501,458.16017 h 0.35937 l 0.60157,0.41797 0.96093,-0.41797 h 0.35938 l 0.29687,0.30078 0.30079,0.11719 0.35937,-0.29688 0.30078,-0.12109 h 0.30078 l 0.35938,0.12109 0.30078,0.17969 0.66016,-0.30078 h 0.30078 L 507,458.46094 h 0.35937 l 0.30079,-0.30078 h 0.35937 l 0.60156,0.30078 h 0.35938 l 0.59765,-0.30078 0.36329,0.12109 0.29687,0.17969 h 0.30078 l 0.66016,-0.30078 0.53906,0.41797 0.66016,-0.41797 0.36328,0.12109 0.29687,0.17969 0.30078,0.11719 0.35938,-0.29688 0.30078,-0.12109 0.66016,0.41797 0.60156,-0.29688 h 0.35937 l 0.59766,0.29688 0.24219,-0.29688 0.41797,-0.12109 0.24218,0.23828 0.41797,0.0625 0.54297,-0.30078 h 0.29688 l 0.36328,0.23828 h 0.29687 l 0.36328,-0.23828 h 0.29688 l 0.30078,0.12109 h 0.35938 l 0.60156,-0.17969 0.66015,0.17969 0.66016,-0.17969 0.30078,0.0586 h 0.53906 l 0.41797,-0.0586 0.24219,0.0586 h 0.71875 l 0.24219,-0.0586 0.35937,0.0586"
%% Conversion to coordinates
str=ans;
cmd = regexp(str,'[MmLlHhVvCcSs] [^MmLlHhVvCcSs]*','match');
x = [];
y = [];
for i = 1:numel(cmd)
cmdi = strsplit(cmd{i},' ');
switch cmdi{1}
case 'M'
coords = regexp(cmdi{2},'(\S*),(\S*)','tokens');
x = [x str2double(coords{1}{1})];
y = [y str2double(coords{1}{2})];
case 'm'
% m commands can occur multiple times in a row
for j=2:numel(cmdi)-1
coords = regexp(cmdi{j},'(\S*),(\S*)','tokens');
if i==1
x = [x str2double(coords{1}{1})];
y = [y str2double(coords{1}{2})];
else
x = [x x(end)+str2double(coords{1}{1})];
y = [y y(end)+str2double(coords{1}{2})];
end
end
case 'L'
% L commands can occur multiple times in a row
for j=2:numel(cmdi)-1
coords = regexp(cmdi{j},'(\S*),(\S*)','tokens');
x = [x str2double(coords{1}{1})];
y = [y str2double(coords{1}{2})];
end
case 'l'
% l commands can occur multiple times in a row
for j=2:numel(cmdi)-1
coords = regexp(cmdi{j},'(\S*),(\S*)','tokens');
x = [x x(end)+str2double(coords{1}{1})];
y = [y y(end)+str2double(coords{1}{2})];
end
case 'H'
coords = regexp(cmdi{2},'(\S*)','tokens');
x = [x str2double(coords{1}{1})];
y = [y y(end)];
case 'h'
coords = regexp(cmdi{2},'(\S*)','tokens');
x = [x x(end)+str2double(coords{1}{1})];
y = [y y(end)];
case 'V'
coords = regexp(cmdi{2},'(\S*)','tokens');
x = [x x(end)];
y = [y str2double(coords{1}{1})];
case 'v'
coords = regexp(cmdi{2},'(\S*)','tokens');
x = [x x(end)];
y = [y y(end)+str2double(coords{1}{1})];
otherwise
warndlg(['SVG Command ' cmdi{1} ' unknown.']);
end
end
origin(1) = h(1,1)- (h(2,1)-h(1,1))/(h(2,2)-h(1,2))*h(1,2);
origin(2) = v(1,1)- (v(2,1)-v(1,1))/(v(2,2)-v(1,2))*v(1,2);
x = x + t(1) - origin(1);
y = y + t(2) - origin(2);
x = x*(h(2,2)-h(1,2))/(h(2,1)-h(1,1));
y = y*(v(2,2)-v(1,2))/(v(2,1)-v(1,1));
%% control plot
figure
plot(x,y)
%% export coordinates to txt file
x = x';
y = y';
expo = [x y];
isOctave = exist('OCTAVE_VERSION', 'builtin') ~= 0;
if isOctave
save("-ascii",strftime("%Y-%m-%d-%H-%M-%S.txt", localtime (time ())),"expo")
else
save("-ascii",datestr(now,'YYYY-mm-DD-HH-MM-SS.txt'),"expo")
end
Please register or sign in to comment