KevsRobots Learning Platform

BrachioGraph Tutorial

84% Percent Complete

Vectorize Images

Calibrate the BrachioGraph

By Kevin McAleer,    3 Minutes


Using the linedraw.py Module to Vectorize Images

The linedraw.py module allows you to convert bitmap images into vector format. This can be useful for creating images that your BrachioGraph can draw. The module helps you convert a bitmap image file into an SVG file for visual verification and a JSON file for use with the BrachioGraph.


Installation

First, you’ll need to install the opencv-python-headless package, as it is not included in the standard requirements.txt. You can install it using the following command:

pip install opencv-python-headless

Using Key Functions

Launch a Python shell and import the necessary functions from linedraw:

from linedraw import *

Convert an Image to JSON Using image_to_json()

To convert an image to a JSON file, you can use the image_to_json function. For instance, to convert an image named africa.jpg located in the images directory, you can use the following command:

image_to_json("images/africa", draw_contours=2, draw_hatch=16)

This command performs the following tasks:

  • Finds the file named africa (or africa.jpg, africa.png, africa.tif) in the images directory.
  • Draws its contours and hatch lines.
  • Creates a JSON file named africa.json (or africa.jpg.json, etc.).
  • Creates an SVG file named africa.svg (or africa.jpg.svg, etc.).

A draw_contours or draw_hatch value of 0 will disable those features. Lower values provide more detail, while higher values provide less. Experiment with these values to find the best settings:

  • Start with a draw_contours value of 2, then try values between 0.5 and 4.
  • Start with a draw_hatch value of 16, then try values between 8 and 16.

Emphasize the Edges

You can emphasize the edges of your image by using the repeat_contours parameter. For example:

image_to_json("images/africa", draw_contours=2, draw_hatch=16, repeat_contours=3)

Setting repeat_contours=3 means that the contour data will be added to the JSON file three times, making the edges stand out more prominently. This is particularly effective with pencil drawings.


Check the Results with SVG

To verify the results, open the generated SVG file. You can draw the JSON file with the BrachioGraph using the following command:

BrachioGraph.plot_file("images/africa.json")

Convert an Image to Lines Using vectorise()

If you want to get the lines in a format that you can process in a shell or script, use the vectorise function:

lines = vectorise("images/africa.jpg", draw_hatch=16, draw_contours=2)

This command generates:

  • A list of lines, each of which is a list of points.
  • An SVG file to give you a visual idea of the vectorized representation.

Refer to the vectorise function documentation for full details on the parameters it accepts.


Visualize the Plotter’s Drawing Sequence with draw()

The draw function allows you to visualize how the plotter will draw the lines using Python’s turtle graphics module:

draw(lines)

This function sequentially draws the lines generated by vectorise(). Although it is slower than the actual plotter, it provides a useful preview of the drawing process.

With these tools and techniques, you can effectively convert and optimize images for your BrachioGraph, ensuring that your drawings are accurate and detailed.


< Previous Next >