bitmap image to paint stroke g-code - part 1

A project log for If ( ) Then {Paint}

a machine to create canvas paintings of your favorite digital images

John OpsahlJohn Opsahl 08/22/2019 at 21:260 Comments

Four days, seven coffee shops, and 750 lines of code later and I am getting very close to finalizing the Python project that can generate the If Then Paint prototype machine g-code from bitmap images. It's been a little over two years since I first starting developing the code. I figure it's time to finally describe the process from image to g-code and detour to some of my favorite challenges that I encountered along the way. 

It starts with an bitmap image. In this case, it is a picture of a tree in a meadow that my wife took on our last vacation.

The bitmap image then gets sized to match the aspect ratio of the physical canvas and resolution of the digital canvas (see sized image below). If we didn't want to lose the proportions of the original image, we can just open the original image in an image manipulation program, cut out the desired section of the image with the same aspect ratio as the physical canvas, save the section as a new image, treat the new section image as a new original image, and perform the sizing process. I have chosen not to keep the original image proportions to make it easier to visualize what the resizing process is doing. The image is sized to the resolution of the digital canvas because a spatial reference between pixels and millimeters is needed. The If Then Paint prototype uses a 768x1024 digital canvas resolution for a 120x160mm physical canvas (i.e. 6.4 pixels per mm).

The next task is to simplify the image to only a few colors using color quantization. The image below is reduced to just five colors. The primary reason for reducing the number of colors is to reduce the number of paint colors that need to be mixed to create the image. The original bitmap image might have hundreds of colors. Mixing hundreds of colors will take a long time and the final painting might actually look better with fewer colors.

After color quantization, we can start to apply the secret sauce of this image to g-code recipe - the algorithm that generates paint stroke options from the bitmap image. With a simple horizontal and vertical scan of the dark green color, we get the following possible paint strokes (shown in purple for contrast but also for fun). The next project log will dive deeper into the details of the algorithm.