Openscad: Tracing a Custom Line in a Photo Image

About: Retired but working.

For various reasons, I wanted to find a particular irregular path in a photograph. With the result to be an openscad array of the XY datapoints tracing along the line's path.

It was acceptable to require manually tracing the path in Photoshop, brushing out a mono-color line along the desired path, using a paintbrush. This manual tracing of the color line requires the line to be 4 pixels wide, and such must be the setting of the paintbrush in Photoshop.

I generalized a bit, to a) allow multiple, non-overlapping lines per image, by b) putting a row of color blocks along the bottom of the image, where each color block is the trace line color, blocks terminated by a white block.

Then the function searches along the left margin of the image to find the start of a line trace matching the first color. It traces the line, recording the points.

Then repeat for the next color block.

Lines cannot cross, in this version!

The first image shows the resulting openscad lines, on top of a surface() of the image.

Teacher Notes

Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.

Step 1: Converting the Image to an Openscad Array

Python script png-rcol takes one argument, a .png file, and converts it to an openscad array. This array has one row for each pixel row. Each pixel row is a series of 3 color intensities R, G, and B, at each pixel.

The output of png-rcol is redirected to a text file.

python3 SF_Topo.png > SF_Topo.txt

Step 2: Openscad Functions

The main functions of this script are invoked in the example at the end of the file.

1. include the image.txt file and fix the assignment to use the array name created by The array name is the name of the .png file, with "-" replaced by "_".

2. edit the surface() function to name the .png file.

3. function LT_ColorTraceList() searches for the row of color boxes at the lower left of the image, terminated by a white box.

4. function LT_buildTopLineList() looks for each line in turn, using the specified starting point. It returns an array of line points, one array per color line.

5. function ShowLinePoints() is for debugging, to show the results of finding the color lines.

Be the First to Share


    • Art Skills Challenge

      Art Skills Challenge
    • Make it Move

      Make it Move
    • Teacher Contest

      Teacher Contest