-
I am looking for library for implementing svg rendering in my project, PyGerber. It seems like this library is best choice for this purpose. Unfortunately Gerber file format requires subtraction from arbitrary parts of image. It can be done in dumb way, just by writing over the content with background color (assuming it is possible to control order of rendering of paths/shapes based on some kind of z-index), but it would be much better to actually remove parts of image. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
This is possible with SVG masks: import drawsvg as draw
d = draw.Drawing(200, 200)
# Example based on https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/mask#example
# A mask is a black-and-white image and the black parts specify where to cut
# holes in the target image
hole_mask = draw.Mask()
# First make everything white, adjust coordinates to match your drawing
hole_mask.append(draw.Rectangle(0, 0, 200, 200, fill='white'))
# Add black shape(s) to define the hole, in this case a star-shaped hole
hole_mask.append(draw.Lines(
100, 46, 132, 143, 47, 83, 152, 83, 67, 143,
closed=True,
fill='black'))
d.append(draw.Circle(100, 100, 36, fill='SkyBlue'))
d.append(draw.Circle(100, 100, 90, fill='red', mask=hole_mask)) # Shape with cutout
# d.rasterize() does not currently show `mask` correctly
d.display_inline() Unfortunately, import drawsvg as draw
def draw_poly(path, *xy_list):
path.M(*xy_list[0])
for x, y in xy_list:
clip_shape.L(x, y)
path.L(*xy_list[0])
d = draw.Drawing(200, 200)
# Example based on https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/mask#example
clip = draw.ClipPath()
clip_shape = draw.Path(fill='black', fill_rule='evenodd')
# Draw boundary rectangle
draw_poly(clip_shape, (0, 0), (0, 200), (200, 200), (200, 0))
# Draw cut hole shape, this cannot be a self-intersecting path like the star shown earlier
draw_poly(clip_shape, (100, 46), (132, 143), (67, 143))
clip.append(clip_shape)
d.append(draw.Circle(100, 100, 36, fill='SkyBlue'))
d.append(draw.Circle(100, 100, 90, fill='red', clip_path=clip)) # Shape with cutout
d.display_inline()
d.rasterize() |
Beta Was this translation helpful? Give feedback.
This is possible with SVG masks: