This repository contains tools for converting SVG and PNG images to G-code for use with laser engravers. This project was created to support hobbyists and professionals in laser engraving, providing an easy way to convert common file formats to G-code.
Converts SVG paths to G-code.
python svg_to_gcode.py <svg_file_path> <output_gcode_path> --power=<laser_power> --feed_rate=<engraving_speed> --longest_side=<max_length_of_cut> --center_offset=<X_offset,Y_offset>
<svg_file_path>
: Path to the input SVG file.<output_gcode_path>
: Path to save the generated G-code.--power
: Laser power setting (default 1000).--feed_rate
: Feed rate (speed) for engraving (default 1000).--longest_side
: Optional. Length of the longest side of the engraving in millimeters.--center_offset
: Optional. X and Y offset in millimeters to center the cut.
Converts PNG images to G-code by tracing the image contours.
python image_to_gcode.py <image_path> <output_gcode_path> --power=<laser_power> --feed_rate=<engraving_speed> --longest_side=<max_length_of_cut> --center_offset=<X_offset,Y_offset> --threshold=<threshold_level>
<image_path>
: Path to the input .png image file.<output_gcode_path>
: Path to save the generated G-code.--power
: Laser power setting (default 1000).--feed_rate
: Feed rate (speed) for engraving (default 1000).--longest_side
: Optional. Length of the longest side of the engraving in millimeters.--center_offset
: Optional. Center offset (X, Y) in millimeters to align the center of the scaled cut.--threshold
: Optional. Thresholding level for processing the image (default uses adaptiveThresholding).
Clone the repository and navigate to the repository directory:
git clone https://github.com/Khlick/gcode-converter.git
cd gcode-converter
To use these tools, we recommend creating a Python environment using venv
or conda
. Activate the environment and install the required Python packages.
python -m venv venv
source venv/bin/activate # On Windows use `venv\Scripts\activate`
pip install -r requirements.txt
conda create --name myenv python=3.8
conda activate myenv
conda install --file requirements.txt
Produce a cut of the following svg:
python svg_to_gcode.py img/example.svg gcode/example1.gcode --power 1000 --feed_rate 600 --longest_side 45 --center_offset (40,40)
Produces the following gcode (read into LaserGRBL v5.7.0):
Produce a cut of the following svg:
python image_to_gcode.py img/example2.png gcode/example2.gcode --power 1000 --feed_rate 600 --longest_side 50 --center_offset(20,20)
Produces the following gcode (read into LaserGRBL v5.7.0):
- Python (3.12.0): A high-level, interpreted programming language known for its ease of use.
- OpenCV-Python: A library of Python bindings designed to solve computer vision problems.
- Matplotlib: A comprehensive library for creating static, animated, and interactive visualizations in Python.
- Numpy: A fundamental package for scientific computing with Python.
- svgpathtools: A collection of tools for manipulating and analyzing SVG Path objects and Bezier curves.
To install these dependencies, run the following command:
pip install opencv-python matplotlib numpy svgpathtools
Alternatively, you can use the provided requirements.txt
file with the following command:
pip install -r requirements.txt
For more information on the usage and capabilities of the tools used, please refer to the official documentation:
- Python Documentation
- OpenCV Documentation
- Matplotlib Documentation
- Numpy Documentation
- svgpathtools Documentation
Please ensure you have the correct version of Python installed and the above packages updated to their latest versions to avoid compatibility issues.
This project is open-sourced under the MIT License (see LICENSE for details).