Skip to content

About

A Python-based Image to G-code generator for writing machines and CNC plotters. Uses Potrace to convert images into SVG, then converts SVG paths into G-code. Includes a simple Tkinter GUI. Future updates will add laser engraving and 3D printing slicing support.

Resources

Stars

2 stars

Watchers

0 watching

Forks

Latest commit

 

History

2 Commits

Folders and files

Repository files navigation

Writing Machine G-Code Tools

A collection of Python tools designed to generate G-code for 2D plotters, writing machines, and CNC drawing robots. This repository contains two main utilities: a high-precision outline converter using Potrace and an experimental AI-powered generator.

🛠️ Tools Included

1. Potrace G-Code Converter (gcode_converter_potrace.py)

The "Professional" tool. It uses the potrace algorithm to trace bitmap images (JPG, PNG) into high-quality vector paths (SVG) and then converts them to G-code.

  • Best for: Existing images, logos, and high-contrast line art.
  • Features:
    • Live preview of edge detection.
    • Adjustable threshold and scaling.
    • Automatic G-code normalization (fits to target size).

2. AI G-Code Generator (ai_gcode_generator.py)

The "Creative" tool. It can generate images from text prompts using Google's Gemini API and convert them to G-code using internal Python algorithms (Marching Squares).

  • Best for: Creating new designs from scratch ("Sci-fi sheep", "Geometric flower").
  • Features:
    • Text-to-Image: Generates plotting-optimized line art from prompts.
    • Pure Python Tracing: Does not require external tools like Potrace.
    • Generates smoothness and feed rate controls.

📋 Requirements

Python Libraries

Install the required dependencies using pip:

pip install Pillow numpy scipy requests svgpathtools shapely

External Tools

  1. Potrace (Required only for gcode_converter_potrace.py)

    • Windows: Download the binary from potrace.sourceforge.net.
    • Add potrace.exe to your system PATH, or edit the script to point to the executable location.
    • Linux/Mac: Install via package manager (e.g., sudo apt install potrace or brew install potrace).
  2. Google Gemini API Key(if uses ai mode) (Required for ai_gcode_generator.py)

    • Get a free API key from Google AI Studio.
    • Set the environment variable GEMINI_API_KEY on your system.

🚀 Usage

Using the Potrace Converter

  1. Run the script:
    python gcode_converter_potrace.py
  2. Select an input image.
  3. Adjust the Threshold slider until the preview shows clear outlines.
  4. Set the Output Directory and Filename.
  5. Click Convert.

Using the AI Generator

  1. Set your API Key:
    • Windows (PowerShell): $env:GEMINI_API_KEY="your_key_here"
    • Linux/Mac: export GEMINI_API_KEY="your_key_here"
  2. Run the script:
    python ai_gcode_generator.py
  3. Type a prompt (e.g., "Robot cat line drawing") and click Generate.
  4. Once generated, adjust settings and click Extract Outline to G-code.

✅ Todo

  • Add support for batch processing multiple images.
  • Improve the internal "Marching Squares" algorithm in the AI tool to handle sharper corners better.
  • Add a visual G-code viewer/simulator within the app.
  • Create a unified GUI launcher for both tools.
  • Add support for DXF export.

📝 License

Open Source. Feel free to modify and distribute.

About

A Python-based Image to G-code generator for writing machines and CNC plotters. Uses Potrace to convert images into SVG, then converts SVG paths into G-code. Includes a simple Tkinter GUI. Future updates will add laser engraving and 3D printing slicing support.

Resources

Stars

2 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages