pixel2svg有可能做到这一点,但它需要对脚本进行一些修改。
怎么做
您需要修改svgdoc.add
pixel2svg.py 第 125 行的调用以添加 opacity 属性。它应该如下所示:
svgdoc.add(svgdoc.rect(insert = ("{0}px".format(colcount * arguments.squaresize),
"{0}px".format(rowcount * arguments.squaresize)),
size = ("{0}px".format(arguments.squaresize + arguments.overlap),
"{0}px".format(arguments.squaresize + arguments.overlap)),
fill = svgwrite.rgb(rgb_tuple[0],
rgb_tuple[1],
rgb_tuple[2]),
opacity = rgb_tuple[3]/float(255)))
这是代码更改的差异。为了展示它,我制作了这个非常小的图像。这是修改后的 pixel2svg 生成的 SVG(或在此处查看实际操作。):
<?xml version="1.0" encoding="utf-8"?>
<svg xmlns="http://www.w3.org/2000/svg" xmlns:ev="http://www.w3.org/2001/xml-events" xmlns:xlink="http://www.w3.org/1999/xlink" baseProfile="full" height="40px" version="1.1" width="440px">
<defs/>
<rect fill="rgb(255,0,255)" height="40px" opacity="0.101960784314" width="40px" x="40px" y="0px"/>
<rect fill="rgb(255,0,255)" height="40px" opacity="0.2" width="40px" x="80px" y="0px"/>
<rect fill="rgb(255,0,255)" height="40px" opacity="0.301960784314" width="40px" x="120px" y="0px"/>
<rect fill="rgb(255,0,255)" height="40px" opacity="0.4" width="40px" x="160px" y="0px"/>
<rect fill="rgb(255,0,255)" height="40px" opacity="0.501960784314" width="40px" x="200px" y="0px"/>
<rect fill="rgb(255,0,255)" height="40px" opacity="0.6" width="40px" x="240px" y="0px"/>
<rect fill="rgb(255,0,255)" height="40px" opacity="0.701960784314" width="40px" x="280px" y="0px"/>
<rect fill="rgb(255,0,255)" height="40px" opacity="0.8" width="40px" x="320px" y="0px"/>
<rect fill="rgb(255,0,255)" height="40px" opacity="0.901960784314" width="40px" x="360px" y="0px"/>
<rect fill="rgb(255,0,255)" height="40px" opacity="1.0" width="40px" x="400px" y="0px"/>
</svg>
这是怎么回事
pixel2svg 使用PIL处理所提供图像的每个像素。它实际上已经将每个像素的 alpha 值拉为整数 [0-255],它只是没有做太多:
image = PIL.Image.open(positional[0])
print("Converting image to RGBA")
image = image.convert("RGBA")
然后使用svgwrite库将像素数据重建为 SVG 。每个像素都被绘制为svgwrite.shapes.Rect
允许您将“附加 SVG 属性作为关键字参数”链接的方法。SVG 不透明度值需要一个浮点数 [0.0-1.0],因此我们只需要在将其设置为不透明度之前对 alpha 值进行标准化。
免责声明:这是我第一次使用 Python,所以请随时指出我可能犯的任何初学者错误!