Filling color in svg image
Question:
I have an svg file in this format:
<svg rel="nofollow noreferrer">
To fill the SVG svg's all "black" places (path) with solid color of specified hex code I used this:
Example: 'svg42.svg'
to be filled with solid color '#00bbaa'
import io
from PIL import Image
from cairosvg import svg2png
from lxml import etree
def svg2image(file: str, color: str, width: int, height: int):
with open(f'{path}', 'r') as f:
svg_data = f.read().encode('utf-8')
root = etree.fromstring(svg_data)
tree = etree.ElementTree(root)
root.attrib["fill"] = color
for path in root.iter('path'):
path.attrib["fill"] = color
imgdata = io.BytesIO()
tree.write(imgdata)
img = Image.open(io.BytesIO(svg2png(bytestring=imgdata.getvalue(), output_width=width, output_height=height)))
return img
Problem:
when trying to fill the color in a more complex svg, it shows no effect on the output:
<svg width="989" height="875" xmlns="http://www.w3.org/2000/svg" xmlns_xlink="http://www.w3.org/1999/xlink"
xml_space="preserve" overflow="hidden"><defs><clipPath id="clip0"><rect x="902" y="586" width="989" height="875"/></clipPath></defs>
<g clip-path="url(#clip0)" transform="translate(-902 -586)"><rect x="902" y="586" width="639" height="652" fill="#ED7D31"/>
<rect x="1252" y="810" width="639" height="651" fill="#4472C4"/></g></svg>
What am I missing here?
Answers:
import io
from PIL import Image
from cairosvg import svg2png
from lxml import etree
def svg2image(file: str, color: str, width: int, height: int):
with open(f'{file}', 'r') as f:
svg_data = f.read()
root = etree.fromstring(svg_data)
tree = etree.ElementTree(root)
root.attrib["fill"] = color
imgdata = io.BytesIO()
tree.write(imgdata)
img = svg2png(bytestring=imgdata.getvalue(), output_width=width, output_height=height)
img = Image.open(io.BytesIO(img))
return img
Edit:
this was the answer I found to my original question, but it is not very efficient (nor applicable) for the edited question
I have an svg file in this format:
<svg rel="nofollow noreferrer">
To fill the SVG svg's all "black" places (path) with solid color of specified hex code I used this:
Example: 'svg42.svg'
to be filled with solid color '#00bbaa'
import io
from PIL import Image
from cairosvg import svg2png
from lxml import etree
def svg2image(file: str, color: str, width: int, height: int):
with open(f'{path}', 'r') as f:
svg_data = f.read().encode('utf-8')
root = etree.fromstring(svg_data)
tree = etree.ElementTree(root)
root.attrib["fill"] = color
for path in root.iter('path'):
path.attrib["fill"] = color
imgdata = io.BytesIO()
tree.write(imgdata)
img = Image.open(io.BytesIO(svg2png(bytestring=imgdata.getvalue(), output_width=width, output_height=height)))
return img
Problem:
when trying to fill the color in a more complex svg, it shows no effect on the output:
<svg width="989" height="875" xmlns="http://www.w3.org/2000/svg" xmlns_xlink="http://www.w3.org/1999/xlink"
xml_space="preserve" overflow="hidden"><defs><clipPath id="clip0"><rect x="902" y="586" width="989" height="875"/></clipPath></defs>
<g clip-path="url(#clip0)" transform="translate(-902 -586)"><rect x="902" y="586" width="639" height="652" fill="#ED7D31"/>
<rect x="1252" y="810" width="639" height="651" fill="#4472C4"/></g></svg>
What am I missing here?
import io
from PIL import Image
from cairosvg import svg2png
from lxml import etree
def svg2image(file: str, color: str, width: int, height: int):
with open(f'{file}', 'r') as f:
svg_data = f.read()
root = etree.fromstring(svg_data)
tree = etree.ElementTree(root)
root.attrib["fill"] = color
imgdata = io.BytesIO()
tree.write(imgdata)
img = svg2png(bytestring=imgdata.getvalue(), output_width=width, output_height=height)
img = Image.open(io.BytesIO(img))
return img
Edit:
this was the answer I found to my original question, but it is not very efficient (nor applicable) for the edited question