I'm working on a computer science project which is a CNC plotter basically all of the methods I see for getting Gcode uses Inkscape. I have already written software to convert Normal images to black and white edges only and I have pulled the coordinates from the image. Is there any way X,Y coordinates can be used to generate Gcode ? or would i have to use Inkscape.
2 Answers
GCode is just instructions called where you can pass arguments.
The machine will execute the Gcode one by one and interpret it for moving his motors or do regulation depending on his firmware.
So if you want to create Gcode in python, just create a txt file and append commands.
You need to have the Gcode availables instructions of you machine first (here InkScape).
For example in Marlin:
G1 X90.6 Y13.8 ; move to 90.6mm on the X axis and 13.8mm on the Y axis
To get this file in python:
positions = [ # Get your datas of format them like this:
[90.6, 13.8], # Point 1 [x, y]
[10.6, 3.98]
]
with open("myGCode.gcode", "w") as f:
for x, y in positions:
f.write(f"G1 X{x} Y{y} ;\n")
File created content:
G1 X90.6 Y13.8 ;
G1 X10.6 Y3.98 ;

- 1,212
- 6
- 20
-
thank you very much. Do you know how I can get my outputs to print with "," after them, currently they print as (a.b) (c,d) (e,f) but, I want it to print with commas after every coordinate like (a,b), – Okezie Agazie Dec 16 '21 at 16:47
-
You can use the .replace(".", ",") method to do this. PS: don t forget to greenmark and upvote if ok for you! – Vincent Bénet Dec 16 '21 at 19:13
-
Hi, I tried but, i got an atribute error. This error to be specific; v = p_string.replace("]", ",") AttributeError: 'list' object has no attribute 'replace' – Okezie Agazie Dec 17 '21 at 09:53
-
The replace method is to run on string variable. Can you be more precise with you problem please? I am not sure to understand... – Vincent Bénet Dec 17 '21 at 10:14
-
So basically, I print the coordinates of black pixels in a certain image. They are printed as, (12 , 14) (2, 5) etc..... I want them to be printed with commas after every coordinate e.g (12, 4), (2,5), so i could just paste them into the gcode generator straight away. – Okezie Agazie Dec 17 '21 at 11:27
-
Use bash like this: `myscript1.py > output.txt` This will store your printed things. Then in the gcode generator, open this txt file and use replace method or regex https://docs.python.org/3/library/re.html – Vincent Bénet Dec 17 '21 at 11:34
It really depends on the machine and its controller, but most of the time, Linear interpolation like G1 or G01 usually only needs to be specified once, like
G01 X1.0 Y2.0;
And then its linear interpolation enabled already so it can just be
X1.0 Y3.0;
...
Up to the point where you wanna go back to rapid movement (G0 G00)
Or circular interpolation with (G02, G03)
But then it's still usually just coordinates are enough after switching to specific interpolation once.
Yet then I assume its for simple milling and more recent mills (I was trained for Haas) has some fancy pocketing functions, where you just specify few key points for contour and they can kinda be deducted mathematically.
It would be interesting to see your program for getting a contour out of a photo.
But specifying type of interpolation between each set of coordinates is also OK, just it might make code readability slightly more difficult.

- 23
- 5
-
Hi there, my machine is just a 2d plotter it just draws onto a piece of paper don't think i'm good enough to make a mill yet . any idea on how i can add a pen up function so it doesnt plot on the parts of the image which are supposed to be white – Okezie Agazie Dec 17 '21 at 15:42