Wednesday, 20 July 2022

How to combine PIL and cairosvg to create pattern from svg?

I was wondering if would be possible to load a svg format like shape and with loop to repeat with for loop to get some kind a generic pattern. Most what I have founded researching online, is to convert from svg to png or some else format but I was wondering is it possible to manipulate before converting to some format (jpg)?

I have tried to combine cairosvg and PIL but I have not gone too far.

from cairosvg import svg2png
from PIL import Image, ImageDraw

white = (255,255,255)
img = Image.new('RGB', (300,300), white)
draw = ImageDraw.Draw(img)

svg_code = """
    <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="#000" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
        <circle cx="12" cy="12" r="10"/>
        <line x1="12" y1="8" x2="12" y2="12"/>
        <line x1="12" y1="16" x2="12" y2="16"/>
    </svg>
"""

Usually I would use something like this...

for x in range(0, 300, 25):
    draw.svg_code??

But this doesn't work... Any idea how can I load svg format and use it with other modules?

Thanks in advance!



from How to combine PIL and cairosvg to create pattern from svg?

No comments:

Post a Comment