SVG 转黑白
我希望能够将 SVG 文档转换为黑白文档。我的尝试是使用“sed”的以下 Makefile 脚本:
%.bw.svg: %.svg
sed '/stroke:none/!s/stroke:[^;\"]*/stroke:black/g' $< > $@
这适用于行等,但不适用于填充。基本上,如果笔划不是不可见的(无),那么我将其转换为黑色。我想对填充物做同样的事情,如果不是白色或不可见,则转换为黑色。
我想知道以更好的方式(也许使用 XSLT)做这样的事情是否会太复杂,但我没有经验。任何人都可以帮忙吗?
I would like to be able to convert SVG documents to black and white. My try is the following Makefile script using 'sed' :
%.bw.svg: %.svg
sed '/stroke:none/!s/stroke:[^;\"]*/stroke:black/g' lt; > $@
This works for lines etc but not for fillings. Basically if the stroke is not invisible (none), then I convert it to black. I would like to do the same for fillings, if not white or invisible, then convert to black.
I wonder if it would be too complex to do something like this in a better way, perhaps using XSLT, but I have no experience. Anyone can help ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我会尝试的两个选项:
1- Inkscape 似乎能够做到这一点 - Inkscape 转换
2- SVG 支持 SVG 元素上的 ColorProfile 属性,该属性可以引用 ICC 颜色配置文件。我会尝试在那里引用灰度颜色配置文件,看看会发生什么。看起来此处有一个可用。
Two options that I would try:
1- Inkscape appears to be able to do it - Inkscape Convert
2- SVG supports a ColorProfile attribute on the SVG element that can reference an ICC Color Profile. I would try to reference a GrayScale color profile there and see what happens. Looks like there is one available here.
我的第一个想法是,通过 sed 等操作 XML(在本例中为 SVG)可能很危险,因为它无法正确转义 XML 字符或尊重字符编码。
话虽如此,您的数据集可能受到足够的约束和限制,因此这不是一个特殊的问题。
考虑 XPath 解决方案(包括 XSLT)听起来不错,因为您将能够精确地识别想要更改的组件。 XQuery 的一些实现可能在这里有用。
一个非常不同的替代方案是 XMLStarlet - 用于在脚本中处理 XML 的命令行工具集。
最后,您可以使用编程工具集来做到这一点吗? Batik 将是我的第一选择(在 Java 世界中)。
My first thought is that it can be dangerous to manipulate XML (in this case SVG) via
sed
etc. since it won't escape XML chars properly or respect character encodings.Having said that, your dataset may be sufficiently constrained and limited such that this isn't a particular problem.
Considering XPath solutions (inc. XSLT) sounds good since you'll be able to precisely identify the components you want to change. Some implementation of XQuery may be of use here.
A very different alternative is XMLStarlet - a command line toolset used for processing XML in scripts.
Finally, can you use a programmatic toolset to do this ? Batik would be my first choice (in the Java world).
我认为你需要一个完整的 CSS 解析器来为所有 SVG 完成这项工作;但对于“由某些特定矢量编辑应用程序生成的 SVG”,包含样式属性的字符串编辑的 XSLT(正如您现在所做的那样,除了它将正确地坚持样式并避免例如)可能就足够了。
如果您编辑问题来解释您的策略如何因填充颜色而失败,这将很有用。
I think you need a full CSS parser to do this job for all of SVG; but for "SVG as generated by some particular vector editing application", XSLT containing string editing of the style attributes (as you're doing now, except that it will properly stick to the styles and avoid e.g. <text>) might be adequate.
It would be useful if you'd edit your question to explain how your strategy fails for fill colors.
首先:不要尝试使用
sed
执行此操作。编辑 XML 比这稍微复杂一些。您可以在图像上使用 SVG 滤镜效果。 ColorMatrix 滤镜基元可以使图像去饱和。
First of all: Don't try doing this with
sed
. Editing XML is a little more complex than that.You can use an SVG filter effect on the image. The ColorMatrix filter primitive can desaturate an image.