Fun with BASH: Going from bitmap to 3D printed object

One of the joys in owning a Makerbot is that you’re never really more than an hour away from having a print of something that you need. Be that a fashion design idea, toy, body scan of you and your partner, give yourself a hand, or something intricate with interlocking parts that you really just thinks is pretty cool.

An interesting question came across my desk about going from a user created image online to something that is 3D printable. Yes, interesting idea…

misc_challenge_accepted

Requirements

  • potrace – PotraceTM is a tool for tracing bitmap graphics and outputting them as an encapsulated PostScript file (EPS)
  • pstoedit – translates PostScript and PDF files into vector formats
  • OpenSCAD – creates 3D solid CAD objects
  • Operating system based on Linux

If you are a Homebrew user you can also install porace and pstoedit via the package manager. The code and simple documentation is on Github under my account and can be found here:
https://github.com/williammincy/bitmap2stl

What this script does is:

  1. Take in a bitmap file as the input
  2. Create a folder named for the bitmap and move everything into that folder, including context
  3. Save a copy of the bitmap into a vector file (EPS)
  4. Save a copy of the vector file into a DXF file
  5. Create a SCAD file with the instructions for extruding the vector outline 2 millimeters at the center
  6. Run the SCAD file through OpenSCAD to generate an STL file

Along the way a few files are generated but the final STL file is the one that pretty much every 3D printer understands. I debated about removing the other files with but chose to leave them in as they are helpful to show the process and also useful as reusable assets for other projects. In case you just want to see the code and not have to click on a link to see it (yes, that does happen) here is the entirety of the BASH script here as well:

[bash] #!/bin/bash

if [ $# -lt 1 ] then
echo "Error: a file name is required"
exit
fi

filename=(`echo $1 | tr "." "n"`)

mkdir $filename
cp $1 $filename/$1
cd $filename

potrace -b eps -W 2in -H 2in -r 300 -n $1
pstoedit -f "dxf:-mm -polyaslines" $filename.eps $filename.dxf

cat > $filename.scad << EOF
dxf_linear_extrude(file="$filename.dxf", layer="0", height=2, center=true);
EOF

/Applications/OpenSCAD.app/Contents/MacOS/OpenSCAD -o $filename.stl -D ‘quality="production"’ $filename.scad

exit
[/bash]

This was a fun workflow to figure out and is something that I’ll be making use of quite frequently as I put my printer through its paces and expand upon my understanding of fabrication.

Leave a Comment