KevsRobots Learning Platform
84% Percent Complete
By Kevin McAleer, 3 Minutes
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.
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
Launch a Python shell and import the necessary functions from linedraw:
from linedraw import *
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:
africa (or africa.jpg, africa.png, africa.tif) in the images directory.africa.json (or africa.jpg.json, etc.).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:
draw_contours value of 2, then try values between 0.5 and 4.draw_hatch value of 16, then try values between 8 and 16.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.
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")
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:
Refer to the vectorise function documentation for full details on the parameters it accepts.
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.
You can use the arrows β β on your keyboard to navigate between lessons.
Comments