How to fit gcode to x y plotter programatically?


I’m gonna use pySerial to send gcode to arduino from python but my gcode is displayed wide when i visualize it.
I see on some commands like this (G1 X228.268604 Y2.636051;) the X is too big for my plotter.
I can send to my plotter to move max X95 and Y95.
like you can see here:

If using some interface like universal gcode sender it may be possible to add some settings(bed size) to fix that but i would like to send the codes line by line with python, so using pySerial.

Do anyone have an idea for down sizing the gcode to fit to my plotter (US Leter format or A4)?

“fix that” isn’t well defined.

What is creating these gcode files? Usually you resolve this where the gcode is created (e.g. setting max sizes, scaling/segmenting the model) rather than using some utility to modify gcode.

I used this to generate the gcode from svg.
I thought i should set my machine size on the interpreter so on my side.
Rigth now I’m trying a kind of hack by divising all X and Y by around 6, and i got something close to my machine, but not finish yet testing.
Ok it should be better then to check deeper the script who made the gcode.

why not scale the SVG design instead of the generated gcode? Inkscape can even do it on the commandline so it can be scripted:

resize the page to the drawing:
inkscape --verb=EditSelectAll --verb=AlignHorizontalCenter --verb=AlignVerticalCenter --verb=FileSave --verb=FileQuit foo.svg

scale the image:
inkscape -z -e out.png -w 400 -h 400 in.svg


I scaled down the original image end did not work, i still get the same size, on the svg i just changed the attributes width and height and did not realy change.
Ok i will test with inkscape. Thanks for the commads.
By the way is it possible to convert svg to gcode directly in command line with inkscape? This was my primary idea but finally give up.

That’s one reason why I mentioned using the command line to size the page to the design size first and then scale the project. I’ve not tried it yet but I hope it doesn’t just scale the page and leave the design size unchanged.

As for calling the gcode plugin from commandl ine in Inkscape it should be possible. You’d probably best ping the github repo with the plugin to ask about it.

On my old Ubuntu 18.04 I could not get inkscape 1.1 to even save the file from command line but this is supposed to work:

inkscape your.svg --verb=FitCanvasToDrawing --verb=FileSave --verb=FileQuit

Then the idea is to scale the size as I listed earlier. I have to figure out why not even --verb=FileSave is working.

i installed inscape with sudo apt install inkscape then:

pi@raspberrypi:~ $ inkscape out.svg --verb=FitCanvasToDrawing --verb=FileSave --verb=FileQuit
WARNING: ignoring verb FitCanvasToDrawing - GUI required for this verb.

** (inkscape:1739): CRITICAL **: 04:17:52.642: static void Inkscape::FitCanvasVerb::perform(SPAction*, void*): assertion 'ensure_desktop_valid(action)' failed
WARNING: ignoring verb FileSave - GUI required for this verb.

** (inkscape:1739): CRITICAL **: 04:17:52.645: static void Inkscape::FileVerb::perform(SPAction*, void*): assertion 'ensure_desktop_valid(action)' failed

Emergency save activated!
Emergency save completed. Inkscape will close now.
If you can reproduce this crash, please file a bug at
with a detailed description of the steps leading to the crash, so we can fix it.
** Message: 04:17:52.652: Error: Inkscape encountered an internal error and will close now.

Segmentation fault
pi@raspberrypi:~ $

I was looking for the inscape-cli version but I think it doesn’t exist I must have confused it with something else.

try adding " --with-gui" as in:
inkscape --with-gui out.svg --verb=FitCanvasToDrawing --verb=FileSave --verb=FileQuit

I got:

(inkscape:3728): Gtk-WARNING **: 05:49:33.426: cannot open display:

i’m using a headless Raspberry Pi, no monitor, command line only.

ssh over with X11 Forwarding enabled?

Anyway even if it work with X11 Forwarding, my final project should work without ssh so I can’t depend on it.

so you can’t run anything but a headless system for this. ok then maybe something like:
rsvg-convert infile.svg -f svg -w 100 -h 100 -o outfile.svg

Awsome my friend!
After install from sudo apt-get -y install librsvg2-bin
rsvg-convert work very well.
Thank you

1 Like