I am able to…
# Load a png image
pngImage = PIL.Image.open('Test.png').convert('RGB')
# convert to NumPy array
npPixels = np.array(pngImage)
So as to…
# Find the coordinates of all pixels matching the color
coordinates = np.argwhere(np.all(npPixels == color, axis=-1))
However, the results are not what I want, since png does not produce clean edges.

So, I need to work with svg files.
I read…
The svgpathtools library provides functionality to convert SVG path elements into Python objects and subsequently into NumPy arrays. Path elements such as Line, QuadraticBezier, CubicBezier, and Arc are represented as Python classes within the library, allowing for manipulation and analysis.
A Path object is a sequence of these path segment objects, behaving like a list, enabling operations such as appending, inserting, replacing, and slicing.To convert a path into a NumPy array, the library allows conversion of Bezier curve objects to numpy.poly1d (polynomial) objects using methods like CubicBezier.poly(), QuadraticBezier.poly(), and Line.poly().
Here is my code so far:
paths2, attributes2, svg_attributes2 = svg2paths2(filename)
path_poly = []
print(f'number of paths: {len(paths2)}') # output - number of paths: 2
for i in range(len(paths2)):
path = paths2[i]
path_attribs = attributes2[i]
id_string = path_attribs['id']
style_string = path_attribs['style']
# get first 4 characters in string
id_stringType = id_string[:4]
if id_stringType == 'path':
d_string = path_attribs['d']
# Parse the d-string into a Path object
path_object = parse_path(d_string)
# This approach allows for direct manipulation of the path data, such
# as extracting coordinates or modifying the path geometry, as the
# path_object is a sequence of path segments that can be iterated over
# or modified.
# The path_object now contains the geometric segments (e.g., CubicBezier, Line)
print(f'\n***********\n\n{path_object}\n')
# output - Path(CubicBezier(start=(86.224817-264.4882j), control1=(86.104108-264.4882j), control2=(85.977914-264.4832j), end=(85.846028-264.4716j)), ...
for seg in path_object:
print(f'segment : {seg}')
# output - segment : CubicBezier(start=(86.224817-264.4882j), control1=(86.104108-264.4882j), control2=(85.977914-264.4832j), end=(85.846028-264.4716j))
print(f'poly - {seg.poly()}')
# output - poly - 3 2
# (-0.000207 + 0.0016j) x + (-0.01645 + 0.015j) x - 0.3621 x + (86.22 + -264.5j)
# Create a Path with a CubicBezier segment
bezier_path = Path(seg)
# Access the CubicBezier segment directly from the path
cubic_bezier_segment = bezier_path
# Verify the type
print(type(cubic_bezier_segment)) # # output - <class 'svgpathtools.path.Path'>
# SHOULD Output: <class 'svgpathtools.path.CubicBezier'>
# bezier_curve = seg
# bezier_path = Path(bezier_curve)
# The isinstance() function checks if a segment is a CubicBezier
if isinstance(seg, CubicBezier):
# The CubicBezier object is already a Bezier curve defined by
# its control points
# Access the control points: start, control1, control2, end
start = seg.start
control1 = seg.control1
control2 = seg.control2
end = seg.end
print(f"CubicBezier: Start={start}, Control1={control1}, Control2={control2}, End={end}")
# You can now use these points to create a new CubicBezier curve
new_bezier = CubicBezier(start, control1, control2, end)
# # Convert the Bezier curve to a polynomial
# (returns a numpy.poly1d object)
# The result is a numpy.poly1d object representing the Bezier
# curve in standard polynomial form
nPoly1dObj = new_bezier.poly()
print(f'numpy.poly1d object : {nPoly1dObj}')
# Create a polynomial object
nPolynomialObj = np.poly1d(nPoly1dObj)
# ((((((((((((((((((((((((((()))))))))))))))))))))))))))
npPixels = np.array(nPolynomialObj)
colorR = 0.0
colorG = 255.0
colorB = 0.0
colorA = 255.0
# color = '#00ff00'
color = [colorR, colorG, colorB, colorA]
# Find coordinates of all pixels matching the color
coordinates = np.argwhere(np.all(npPixels == color, axis=-1)) # np.newaxis
if len(coordinates) > 0:
print(len(coordinates))
for co in range(len(coordinates)):
# Output the coordinates
print(coordinates[co])
xx, yy = coordinates[co]
# ((((((((((((((((((((((((((()))))))))))))))))))))))))))
What I was hoping to accomplish did not materialize.
I was hoping that after creating a polynomial object, I could get the coordinates within the segments.
Can someone help me with this, please.
I just want to get the coordinates of the path object based on the color.