压缩SVG图形

SVGCompress的Python项目详细描述


亮点

svgcompress是一个纯python模块,用于简化/压缩svg(scalable vector graphics)文件。你有没有试过用矢量格式(pdf、svg、eps等)输出一个图,却惊讶于你的文件有10或20mb的重量?需要提交矢量图以供发布,但超出了文件大小限制?在你试图摆脱在矢量中嵌入光栅图像并希望日记本不会注意到这一老习惯之前,先试试svgcompress!SVGCompress可以帮助您缩减文件大小:

  • Removing tiny polygons - Reduce the number of polygons in your image by removing those below a small threshold size. The size threshold can be based on polygon area or circumference.
  • Simplifying shapes - Reduce the complexity of your polygons using the Ramer–Douglas–Peucker algorithm.
  • Merging adjacent or overlapping shapes - Merging can be accomplished by taking the union of overlapping polygons or through the construction of a minimum convex hull.
  • Optimizing with Scour - SVG compress provides a scripting interface for the package Scour, which can optimize/sanitize an svg by removing redundant nodes, deleting comments, simplifying node ids and more. See http://pypi.python.org/pypi/scour for details.

安装

svgcompress只在python 2.7中测试过

step 1:安装numpyshapely

  • Depending on your environment, pip may have issues installing Numpy and Shapely (See Important below). As a result, I’ve placed these two libraries outside of the install_requires list. Please make sure you have these installed through whatever means works for you (Numpy can be installed through pip or through their website, Shapely can be installed through pip if you run a Windows OS or through an application such as Canopy).

步骤2:通过pip安装svgcompress

$ pip install SVGCompress

需要以下非标准库:

  • Numpy
  • svg.path
  • Shapely
  • rdp
  • Scour
  • lxml

重要

svgcompress依赖于shapely,这需要geos框架(http://trac.osgeo.org/geos)。如果在Windows上操作,PIP将与shapely一起安装所需的文件,但在其他操作系统上不会发生这种情况(您将看到错误oserror:在尝试运行时找不到库geos或加载其任何变体)。对于非windows用户,安装geos框架的最方便的方法是通过一个程序,比如canopy(有学术许可证的免费)或类似的空闲软件包管理器。

使用说明

使用svgcompress是通过类compress,或者通过方便函数compress@by@method。此外,还可以通过函数optimize\u svg访问要搜索的脚本接口。函数svg-compress.test包含演示所有使用compress-by-u-method的压缩方法的使用示例。

按方法压缩api:

方便的SVG文件一步压缩功能。

输入:

filename (str) - Path to svg file to compress, e.g. ‘test_vector.svg’

compression_type (str) - how to carry out the compression. Options are ‘delete’ (remove polygons by size), ‘simplify’ (simplify polygons), and ‘merge’ (merge neighboring or overlapping polygons).

curve_fidelity (int) - All polygons affected by compression (e.g. those that are simplified) need to first be linearized so as to convert smooth curves into a set of discrete coordinates. curve_fidelity sets the number of coordinates to use to interpolate a curve. The larger the number, the more computation the code will need to perform. Optional, defaults to 10.

outputfile (str) - Path to output svg. Optional, defaults to ‘originalname_compressed.svg’

pre_select (bool) - If True, will perform the compression only on a subset of the polygons in the svg that are below a certain size threshold. Optional, defaults to False.

selection_tuple (tuple) - If pre_select == True, the selection tuple (‘criteria’, threshold) determines the criteria for selection (‘bboxarea’ for the area of the bounding box of a polygon, or ‘circumference’ for its circumference) and the threshold size (e.g. 100). Optional, defaults to (‘’, ‘’)

optimize (bool) - If True, use package Scour to optimize/sanitize compressed svg (e.g. remove redundant nodes, delete id attributes). Optional, defaults to True

optimize_options (dict) - if optimize == True, use optimize_options to pass args to vector_utils.optimize_svg. See optimize_tothe_max in vector_utils.optimize_svg for default name, value pairs.

kwargs - Depending on the compression type and the pre-selection criteria, additional key word arguments may be required. See compress_by_merging, compress_by_deletion, and compress_by_simplification in class Compress for the valid keyword arguments when compression_type = ‘merge’, ‘delete’, and ‘simplify’ respectively.

输出:

Compressed svg in the directory given by outputfile or in the code directory with the name ‘originalname_compressed.svg’.

If optimized == True, an optimized svg in the directory ‘outputfilename_opt.svg’

A report string outputted to the console will also state some general indicators about the success of the compression, the path of the outputted svg, and the initial and compressed file sizes.

生成/转换SVG图形

可以通过matplotlib生成svg格式的图形。如果您有一个图形编辑器,例如inkscape(免费)或adobe illustrator,它允许您直接在svg中修改文本或行的字体和颜色,而不必重新运行代码,那么这一点尤其方便。您还可以使用inkscape将一个svg文件转换为其他矢量格式,如pdf或eps。 要允许inkscape和其他svg编辑程序将matplotlib图形中的文本识别为文本,在保存svg之前,需要在代码中放置以下代码片段:

matplotlib.rcParams['svg.fonttype'] = 'none'

这也将防止svgcompress试图简化图形中的文本。

示例

svgcompress/test目录包含三个不同文件上每个压缩算法的示例:一个是演示图形(test_vector.svg),第二个和第三个是实际矢量图(map_test.svg,matplotlib_test.svg)

例如,运行以下对svgcompress.compress_by_method的调用

compress_by_method(filename = 'test_vector.svg', compression_type = 'merge',
                                  curve_fidelity = 10, pre_select = True, selection_tuple = ('bboxarea', 300),
                                  epsilon = 5, bufferDistance = 5, operation_key = 'hull')

使用ramer–douglas–peucker算法将test_vector.svg演示文件从87 KB压缩到14 KB,方法是围绕相邻的小多边形(边界框区域<;1000像素)构造凸外壳(操作_key='hull'),以减少多边形的总数,然后使用包screar优化生成的svg。

https://dl.dropboxusercontent.com/u/35392962/demo_diagram.jpg

“test”文件夹中的“test_vector series”包含其他压缩例程的示例。请注意,这些示例旨在使压缩期间发生的更改变得明显。有关更微妙的压缩示例,请参阅map_test系列。

版本

0.60-未经广泛测试。请给我发电子邮件让我知道任何问题。

变更日志

^{str 1}0.60美元(2014年7月21日)

  • Added optimize_svg() to default import from __init__.py and added the ability to pass arguments to it to control how to run the optimization.
  • Fixed issue where optimize_svg() could not read unicode characters, added support for specifying the source encoding
  • Fixed create_testplot.py to output text in test svgs as text rather than paths

^{str 1}0.51美元(2014年7月24日)

  • Moved _get_kb() out of class Compress
  • Moved optimize() out of class Compress to allow it to be called independently
  • Removed numpy and shapely from install_requires and added new installation instructions

^{str 1}0.50美元(2014年7月24日)

  • Added optimize functionality using package Scour
  • Switched from built-in package xml to external library lxml for xml parsing

^{str 1}0.20美元(2014年7月23日)

  • Substantially expanded docstrings

^{str 1}0.19美元(2014年7月22日)

  • Changed compress_by_merging default behavior to group_by_color = True
  • Appended README with critical information on Shapely installation

^{str 1}0.18美元(2014年7月22日)

  • Fixed issue with clipping paths - Code previously threw an exception when trying to extract coordinate data from clipping paths
  • Updated README with a usage example
  • Fixed bug in install_requires that crashed installation with pip

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
Hibernate:jpamodelgen生成java而不是类   java如何在第二次活动结束后显示广告?   javascript如何在linux或windows上将java导出到jar   java One ArrayList添加了2个不同的选项   AmazonWeb服务如何在java中使用AWS Textract检索pdf中存在的表   java为什么RecycleView中的水平项在单击时不起作用?   java计算如果存在映射   java在捕获的组上应用正则表达式   如何使用Java在MySQL的同一个表中插入来自不同类的值   java字符串中最常见的字母(大写和小写)   Spring SessionBean实例正在由java中的多个用户共享   使用Spring Boot将@WebInitParam中的值外部化   java一创建线程就开始执行(多线程)   java是安卓所需的系统权限。意图行动给你打电话?