Processing sketch to transform gcode file to adapt z axis according to a heightmap of a greayscale image.
Load the grayscale image – This will serve as the heightmap. Read the G-code file – Extract X and Y coordinates. Map brightness to Z values – Look up pixel brightness at the corresponding (X, Y) and use it to set the Z value. Output modified G-code – Write a new G-code file with the adjusted Z-axis values.
How It Works
Reads the G-code to find the min/max X and Y values.
Calculates the aspect ratios of both the G-code and the image.
Determines how to fit the G-code into the image while maintaining proportions:
If the G-code is wider, it fits to width and centers in height.
If the G-code is taller, it fits to height and centers in width.
Maps X and Y with padding to properly align with the image.
Adjusts the Z-axis based on the brightness of the corresponding pixel.
Writes a new G-code file with the modified Z values.
When to Use This?
✔ Works great for CNC engraving, laser etching, or 3D printing where Z height should follow a heightmap. ✔ Useful when working with G-code files of unknown size or scale. ✔ Prevents distorted or stretched output.