One step conversion of an image to gcode for Makerbot Unicorn and Reprap style 3D Printers

How to take an image like this:

and in one step generate the gcodes required to do this:

All credit for cad.py goes to the original creator Neil Gershenfeld of  MIT Center for Bits and Atoms and David Carr of Make Your Bot who optimised the code that is in use here.

By attaching a pen to a 3D printer it is easy to turn it into a basic pen plotter. However producing the G-codes require to drive your 3D printer as a pen plotter can be anything but easy. This short guide seeks to change that. There are currently a number of ways of producing plotting paths from images. A selection includes:

 Of these options I believe cad.py is by far the simplest as once it is setup you can convert an image directly to gcode in one go with one program and with out the need to use a command prompt. The following is how to install and use cad.py.

Program Installation

Download the following software for your respective OS. Note that python 2.6 must be used for cad.py to work. Either un-install newer versions or dirct cad.py to work with python2.6 only.

Generating the gcodes.

Run the Cad.py that suits your hardware. I have included three types all with the same interface but each outputs slightly different gcodes.

  • (Cad.py) Plotting with Z axis movement.py -> For use with a three axis Cartesian bot such as a reprap, Makerbot etc. where the pen is lifted off the page due to the movement of the Z (vertical) axis.
  • (Cad.py) Plotting with solenoid or laser.py ->For use with a 2 axis bot where the pen is lifted of the page by an action such as a solenoid or a cutting action like a laser is required. (Use fan port for solenoid as it is switched on off with gcode M106 and M107. Commands need to be switched for use with a laser, see “Optimisation” below.)
  • (Cad.py) Plotting with Makerbot Unicorn.py -> For use with Makerbot Unicorn system which uses a servo to lift the pen off the page.
If you see a command prompt for a split second and then nothing when you run one of the above then recheck you have all the correct programs installed and the correct version of python (2.6!).

When the program begins do the following to test your setup:

  1. Select the test image.png file by clicking the “Input File” button and navigating to the “Test Images” folder.
  2. Set “in. per unit”: 25.4
  3. Click “Cam” button
  4. Click “Output Format” and select file type: “.gcodes”
  5. Set the following values:
    1. maximum vector: 0.75
    2. tool diameter: 0.03
    3. tool overlap: 0.1
    4. contours: 1
  6. Click “contour” and wait for the paths to be generated. They appear as red lines over the image at its edges.
  7. Click the save butting and the file will be output to the input directory.

Thats it, your done. Now load up your desired host, run the gcode and watch your bot draw. Here is a timelaps of my plotter at work.

Optimisation

Now that you hopefully have the basics out of the way you may want to know a little more detail. It appears that cad.py takes a black and white image, performs edge detection on the black areas and then calculates tool paths based on that. When choosing your image I recommend you keep its size under 1000pixels or else the rendering time will be come very long (hours+). For best effect use images with clear distinctions between white and black. Although other image formats such as .jpg do work they dont appear to give the same results as png images. Therefore I recommend you convert .jpg to .png and make sure the image is in black and white with high saturation using your desired image software such as irfanview. When the plotting is started the image will be plotted in the positive dictions from where ever the pen is when printing begins. Therefore you can print many images on the same page by moving the pen to a new position between prints.

Furthermore you can copy and past past multiple gcode files into one as long as you add the new starting position at the end of each file. As mentioned above you have a number of variables to play with in Cad.py. To the best of my knowledge this is their functions:

  • Window sizes – Changes the size of the display window of cad.py. A larger window can make it easier to see when you have the settings right.
  • X Width and Y Width – The size of the image when printed. Roughly equates to the size of the image printed in cm when a .png file is used. Best to do a ‘dry run’ with out a pen first to check the size of your image then adjust accordingly.
  • Zmin and Zmax – The distance the pen is lifted using the z axis (Works for (Cad.py) Plotting with Z axis movement.py  only)
  • Intensity min/max – Change these to change the level of darkness that cad.py interpreted as an edge in an image. Often a low “Intensity Max” value will help the edge detection pickup less distinct areas.
  • Inches per unit – At 24.5 roughly 1cmx1cm image should plot an 1cmx1cm image. Change the value to change the size of your image.
  • Maximum vector fit error – How closely the edge detection fits to the image. Very small values (eg <0.2) take longer to process and can lead to jagged edges. Very high values (eg >1) can lead to corner cutting.
  • Tool Diameter – The most important parameter. The size of the pen end. Lower values mean more detail but longer processing time. To big a value (eg 0.05) and you will not see the detail. Too small (0.001) and you will get artefacts in the edge detection seen as small boxes and dots.
  • Tool overlap – How close two plotted paths are next to each other when more than one contour is used (see below)
  • Contours – The number of layers of infill that will be plotted in dark areas. Set to 1 only the outline will be plotted while -1 will give a complete infill.
  • Feed rate, spindle speed and tools – all not used.

If you wish to go even further then use a text editor to modify the python script its self (eg (Cad.py) Plotting with Z axis movement.py). Remember to back up the original before making changes.

  • Movement speed – Open with text editor, Use Replace All (Crtl + H for notepad) to replace “F2000″ with your desired feed rate (eg F1500) and for the x/y axis or replace all “F300″ for z axis. Then save the file. Gcode generated will now use that speed.
  • Reverse solenoid direction – Replace all “M106 S255 (Pen Up)” with “M106 (Pen Down)” and vice versa to change the solenoid direction or for using a laser. Currently the solenoid is off when the pen is down.
  • Change unicorn serve travel distance – Change the S value (eg S50 to S60) in the “M300 S50 (Pen Up)”  and “M300 S40 (Pen Down)” commands to change the servo travel distance.
  • Change delay time after pen up and down – Replace all “G4 P120″ commands with your desired delay time in miliseconds. Eg P120 (120 miliseconds) to P150 (150 miliseconds.
I have only tested the output gcodes with the repetier firmware and host and using a solenoid as the pen lift method.

Finally as cad.py was originally designed to create tool paths for milling there is nothing to stop you from attaching an engraver, laser or similar and engraving your favourite pattern or quote on something interesting.

About Richard

I am a PhD candidate in Materials Engineering located in Melbourne, Australia.
This entry was posted in A Reprap Project and tagged , , , , , , , , , . Bookmark the permalink.

4 Responses to One step conversion of an image to gcode for Makerbot Unicorn and Reprap style 3D Printers

  1. carel says:

    Hi Richard, this could solve my problem, however (off course!), I am a simpleton when it comes to anythimg more than excel and word- installing the right py version raises questions- the 2,6,5 is not availble from poython, 2,6,6 is OK? also in my try outs I installed a ton of py programs, can I remove them without hurting maybe other programs- scary- with the commoditisation of the 3d print tech, it it noticeable that the software behind it is still in the specialist field

    maybe you can help, I want to do the png to gcode for carving- very low resolution
    a package install with a decent interface would be heaven! ;)

    thx

    Carel

    • Richard says:

      Hi Carel

      Unfortunately my rather unhelpful answer to your questions is that you will simply need to try it and see what works for you. If you keep track of what you change and do one thing at a time then you should be able to reverse any mistakes you make such as uninstalling a required program.

      As for a dedicated installer, it would require a bit of work since every one uses a different OS and this has not really got that many downloads to justify the time involved.

      Anyway, best of luck with your project and I would love some photos of the finished product if you have a link handy.

      Cheers,

      Richard.

    • Joshua says:

      Carel,

      It seems like using the 64 bit version of python won’t work. Some of the links above redirect to more recent versions of code that won’t work together. Go to each site and download the exact file as it is written on this page (“PIL-1.1.7.win32-py2.6.exe” for example).

      Joshua

  2. really awesome! Did just what you said and it worked perfectly! check it out on my blog! http://additive-edge.blogspot.se/ or on my website! http://www.additive-edge.com

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s