Skip to content Skip to sidebar Skip to footer

Converting Numpy Array Svg Image

I have an image which I want to save in svg format. The image is in the form of a numpy array. Although, there exist many methods to save array in different image formats, I could

Solution 1:

Since your image is in a raster format, the best you can do is to convert it to vector graphics with some program like potrace. It has python bindings pypotrace.

Example code:

import numpy as np
import potrace

# Make a numpy array with a rectangle in the middle
data = np.zeros((32, 32), np.uint32)
data[8:32-8, 8:32-8] = 1# Create a bitmap from the array
bmp = potrace.Bitmap(data)

# Trace the bitmap to a path
path = bmp.trace()

# Iterate over path curvesfor curve in path:
    print"start_point =", curve.start_point
    for segment in curve:
        print segment
        end_point_x, end_point_y = segment.end_point
        if segment.is_corner:
            c_x, c_y = segment.c
        else:
            c1_x, c1_y = segment.c1
            c2_x, c2_y = segment.c2

Post a Comment for "Converting Numpy Array Svg Image"