Tracing a monochrome image into a 3D, extruded object with a script
Blender doesn't have image tracing built-in, and without some tracing algorithm that outlines the shape, you're unlikely to get very nice results. 
so suggest...


Open the image in a vector image editor (such as Inkscape)
trace the image (most vector editors have auto-trace)
save the vector as an SVG
import the SVG into Blender
use the curve bevel setting, or convert the curve to a mesh and extrude that.


If you want to automate this, you would have to automate converting the image into a vector outside blender, using something like Potrace, once you have an SVG you can automate loading and extruding it.