View Full Version : Image (Greyscale) to GCODE

12-17-2004, 08:44 PM
Hehe! I have just made a small program that makes gcode of greyscale or B&W images. It is not yet finished, as I don't yet have a CNC mill, but you are welcome to use it and alter or redistribute it, if you find it useful.

It's written in PHP and takes image.png as the image file. Also $x_max, $y_max and $z_max are set to set the image size when carved. I haven't yet found a way for the image not to be mirrored and also there are no way of choosing milling diameter or steps. But look at it, use it and see what you can do:

Mind that all the "n"'s should be preceded by a backslash... Also there might be other small stuff lost in the conversion to this post.


$file = "image.png";
$im = imagecreatefrompng($file);
$size_arr = getimagesize($file);

$x_max = 800;
$x_factor = $size_arr[0];

$y_max = 800;
$y_factor = $size_arr[1];

$z_max = 40;
$z_factor = 256;

$last_x = 0;
$last_y = 0;
$last_z = 0;

echo "G01 X0 Y0 Z0<br>\n";

for ($y=$size_arr[1]; $y>0; $y--) {
for ($x=0; $x<$size_arr[0]; $x++) {
$rgb = ImageColorAt($im, $x, $y);
$z = $rgb;
$x_pos = ($x/$x_factor*$x_max);
$y_pos = ($y/$y_factor*$y_max);
$z_pos = -($z/$z_factor*$z_max);

echo "G01";
if ($x_pos != $last_x){
echo " X".$x_pos;
$last_x = $x_pos;
if ($y_pos != $last_y){
echo " Y".$y_pos;
$last_y = $y_pos;
if ($z_pos != $last_z){
echo " Z".$z_pos;
$last_z = $z_pos;

echo "<br>\n";


12-17-2004, 08:53 PM
Forgot to say, that Gcode output might not be 100% compliant, but it works in CNCSimulator...

02-06-2005, 06:01 AM

I think I'm the first one who tests your software...

I'm also a php programmer. Your code seems to loop very long and I think there are still some errors.

When testing a sample picture I get a Z of: Z-2621439.84375

Shouldn't be correct so I won't test it on my machine yet.

02-28-2005, 10:04 AM
Any new development of this code? I'm not a programer but looking for something for pictures to engrave, Not for buiseness but personal so cost is an issue. and just wondering how this works?

Thanks, Joe

06-10-2005, 10:50 PM

Email me, I'm working on something cool and opensource.
Check out my user (rherman) gallery on this site for pics.


06-11-2005, 09:04 PM
I'm interested in your Gcode program
that generates a numeric grid of your grey-scale image files.

06-27-2005, 11:24 AM
Still working on it! I'll only release it when it is further along. I'll let you know. Thanks.


06-27-2005, 03:45 PM
I'm interested (VERY)... I'd like to try/test it.

Keep us informed of your project rherman.:wave:

07-11-2005, 01:17 AM
Certainly. I have to get back to it. I've been swamped making my rent and bills. Thanks.


07-11-2005, 02:34 AM
Vitrtual Sculpter 3D is a nice grayscale to DNC program with a free 30 day demo version. Do a search on the net if you want to have something for thirty days anyway. It sems like a very good program, I used it a couple of times but my trial ran out.

07-18-2005, 05:54 PM
Yes, I came upon VS3D after I started my program. It is a very good program. I didn't need the cam side of it, however, and I am trying to create algorithms specifically for portraiture taking the hand work out of creating reliefs.


Death Adder
07-21-2005, 11:27 AM
This sounds interesting. The ability to specify tool diameter and pick distance would be nice. I noticed in your PHP code that you step one way through the image array. This is not good because at the end of one line it will gouge through the part to get to the start of the next line (potentially anyways). It would be better to scan through a line, move to the next line and go through that one in the reverse direction. That way you get no gouging.

I might code up something like this myself. I've got a C backend VB front end program that I wrote a while back that does some nifty CNC operations plus does some research related things (like creating octrees and visualizing them in OpenGL). I might try to add something like this to it. Or maybe I'll make it a seperate program.

I hope you don't mind if I try to do what you are doing but in C instead of PHP. A neat thing you might be able to do is make your PHP code accessable over a website so that people can upload their PNG files and get the GCODE output right over the internet. That'd be COOL.

07-22-2005, 05:36 AM
I wrote this about a year ago; I can develop it further if anyone thinks it's worth it!

Death Adder
07-22-2005, 08:30 AM
Well, after one day here is what I've got. Attached is my try at this grayscale image to GCode thing. ;-)

The attached is in EXE and source form. It supports a range of input file types. It's configurable and seems to work for me. I'll keep working on it in the coming days but in the meantime maybe someone can try it and see how it works for them?

10-23-2005, 08:35 PM
chris radek wrote a somewhat usable image to gcode script in Python that accepts a greyscale .gif file as input and works well with EMC.
If you haven't already seen it:

08-02-2006, 11:11 PM
have you guys had a look at "IMAGE-dig" it converts .jpegs (or other formats) to a vectorized graph and produces x, y, z co-ordinates. another good program that might give some ideas is "Profiler-6" (see Attached) it has sorce code included, along with a few stipulations on it's use,

It might give sme ideas a kick

08-14-2008, 04:28 PM
BMP2XYZ program > I wrote this about a year ago; I can develop it further if anyone thinks it's worth it!

I like this program , only the tooth patteren I can not get close enough to make a close cut . Just can not get how the settings work with large picks , 12" by 15" . When I blow it up the cut lines are to far apart . Any seggesions ?

Death Adder
08-15-2008, 07:33 AM
Well, since they didn't post any source code you're probably stuck there... But you could modify one of the open source programs from this thread (I wrote one of them.) My program was a quick VB project but it wouldn't be too hard to make it do interpolation between pixels to get you the smoothness you desire.

10-17-2008, 10:26 AM
I like this program , only the tooth patteren I can not get close enough to make a close cut . Just can not get how the settings work with large picks , 12" by 15" . When I blow it up the cut lines are to far apart . Any seggesions ?

Well, my apologies for waiting so long on this... things sort of fell through the cracks on this; it was more of a "I wonder if this would work" project, and I never really put any finishing touches on it...

I pulled it out this morning for a totally unrelated reason, but did add a few bells and whistles...

If you change the 'stepover' amount or the 'X width' of the cutter path, it will now give an estimate of the number of lines of code. 'Estimated' because part of the application goes back over the generated code and removes redundant moves... such as eight consecutive 'Y' moves at the same 'X' and 'Z' coordinates.

See what you think!

01-27-2009, 05:06 AM
Seems like a popular thing to do! :)

I wrote a trivial one in C a few days ago just to handle the fact that I wanted to use a ball-nose cutter.


Runs on linux, taking an image and converts it to g-code. It's aware of the size of the tool, and cuts to the surface, not into it. So it's able to handle generating a coarse cut, followed by a fine cut.

And of cause it merge multiple steps along a line into a single command. Doesn't yet recognize curves, or do isocutting.

It's actually been used to carve things, so it won't make your machine catch fire. Probably. Maybe.

02-12-2013, 12:09 AM
Yet another depth map (greyscale to g-code) tool:
dmap2gcode (http://home.comcast.net/~sskroch/Dmap2gcode/dmap2gcode.html)

Dmap2gode is based on the image-to-gcode that comes standard with LinuxCNC. Dmap2gcode has some new features and a new user interface. I have tested the output with LinuxCNC and Mach3. I am also providing a precompiled windows executable set for those who do not wish to install Python.

- Supports flat, ball nose and v-bit cutters
- Option to disable cutting of the top surface. (allows stock top surface to remain)
- Option to offset the step over (allows cutting between previously cut scan lines)
- Option for vertical or arc plunge cuts
- Complete set of roughing configuration options
- Origin selection allows user to select the location of g-code zero position
- Use inches or mm as export units
- Customizable g-code preamble and postamble
- Opens previously saved g-code file and retrieve the settings and text
- Roughing g-code is saved to separate file to allow tool change between roughing and finish cut.
- Imports a variety of image files (including jpg, gif, png, pgm)

(When using the .py file it is best if PIL and NumPy are installed on your system. dmap2gcode will function without them but it is painfully slow without NumPy and the image file types and display are limited without PIL. The precompiled windows executable has full speed and capability)

Good luck.

02-13-2013, 06:55 AM