in scripts/awsicons/icon.py [0:0]
def generate_image(self, path, color=None, max_target_size=64, transparency=False):
"""Create image from SVG file and save full color without transparency to path"""
# PlantUML only supports 16 layers of gray causing banding when applying to the
# resource and category icons that have a finer gradient applied. This needs to be replaced
# with a constant background color for the source PNG file before conversion to sprites.
# Parse for id's that indicate service or category and replace with color fill.
# If id is for a resource, no changes needed. Save to a temp SVG file.
ns = {"s": "http://www.w3.org/2000/svg"}
white_rect = etree.Element("rect", width="100%", height="100%", fill="white")
color_rect = etree.Element(
"rect", width="100%", height="100%", fill=f"{self.color}"
)
parser = etree.XMLParser(remove_blank_text=True)
root = etree.parse(str(self.filename), parser)
# Replace any gradient fills with the requisite color
# This was in effect for 2021.01.31 Category icons
elements = root.xpath('//*[@fill="url(#linearGradient-1)"]')
for elem in elements:
elem.attrib["fill"] = self.color
# For resource or category icons which are transparent, set fill to white
# TODO - can we query without namespaces?
elem = root.xpath(
'//s:g[starts-with(@id, "Icon-Resource")]',
namespaces=ns,
)
if elem:
# To set fill, add a rect before any of the paths.
elem[0].insert(0, white_rect)
# For category icons, set fill to category color
elem = root.xpath(
'//s:g[starts-with(@id, "Icon-Architecture-Category")]',
namespaces=ns,
)
if elem:
# To set fill, add a rect before any of the paths.
elem[0].insert(0, color_rect)
# Call batik to generate the PNG from SVG - replace the fill color with the icon color
# The SVG files for services use a gradient fill that comes out as gray stepping otherwise
try:
# Create temporary SVG file with etree
svg_temp = tempfile.NamedTemporaryFile()
svg_temp.write(etree.tostring(root))
svg_temp.flush()
result = subprocess.run(
[
"java",
"-jar",
"-Djava.awt.headless=true",
"batik-1.13/batik-rasterizer-1.13.jar",
"-d",
f"{str(path)}/{self.target}.png",
"-w",
str(max_target_size),
"-h",
str(max_target_size),
"-m",
"image/png",
svg_temp.name,
],
shell=False,
stdout=PIPE,
stderr=PIPE,
)
svg_temp.close()
except Exception as e:
print(f"Error executing batik-rasterizer jar file, {e}")
sys.exit(1)
return