Skip to main content

Compress svg graphics

Project description

SVGCompress

SVGcompress is a pure python module for simplifying/compressing svg (Scalable Vector Graphics) files. Have you ever tried to output a plot in vector format (pdf, svg, eps, etc.) and been surprised that your file weighs 10 or 20MB? Needed to submit a vector figure for publication but run up against the file size limit? Before you try to get away with the old standby of embedding a raster image in your vector and hoping the journal doesn’t notice, try SVGCompress! SVGCompress can help pare down your file size by:

  • 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.

Installation

SVGCompress has only been tested in Python 2.x Installation should be through pip (pip install SVGCompress) Requires the following non-standard libraries:

  • Numpy; svg.path; Shapely; rdp

Usage Notes

Usage of SVGCompress is through the class Compress, or through the convenience function compress_by_method. The function svg_compress.test() contains usage examples demonstrating all of the compression methods with compress_by_method.

Producing your figures in svg format can be done through Matplotlib. This is especially convenient if you have a graphics editor such as Inkscape (free) which will allow you to do things such as modify the font and color of text or lines directly in the svg, without having to re-run your code. You can also use this to convert one of your svg files into an alternate vector format such as pdf or eps.

Examples

SVGCompress/test contains examples of each compression algorithm on three different files: One is a demonstration graphic (test_vector.svg) and the second and third are actual vector plots (map_test.svg, matplotlib_test.svg)

For example, running the following call to SVGCompress.compress_by_method:

compress_by_method(filename, 'merge', curve_fidelity, outputfile, pre_select = True,
                   selection_tuple = (criterias[0], thresholds[0]), epsilon = epsilon,
                   bufferDistance = buffer_distance, operation_key = 'hull')

compresses the test_vector.svg demonstration file from 87 to 30KB by constructing convex hulls (operation_key = ‘hull’) around small neighboring polygons (bounding box area < 1000 pixels) to lessen the total number of polygons and then using the Ramer–Douglas–Peucker algorithm to simplify them. The ‘test_vector series’ in the folder ‘test’ contains examples of other compression routines. Note that these examples were designed to make the changes that occur during compression obvious. For a more subtle example of compression, see the map_test series.

Version

0.18 - Not extensively tested. Please email me to let me know of any issues.

Changes since last version:

  • 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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

SVGCompress-0.18.tar.gz (1.7 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

SVGCompress-0.18-py2-none-any.whl (1.7 MB view details)

Uploaded Python 2

File details

Details for the file SVGCompress-0.18.tar.gz.

File metadata

  • Download URL: SVGCompress-0.18.tar.gz
  • Upload date:
  • Size: 1.7 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for SVGCompress-0.18.tar.gz
Algorithm Hash digest
SHA256 34a9413ba8abbc19ee136f86dbee606b43dd1663a1e50bdeb44d8e53cb247743
MD5 5d8b55f537653c8baba65f7d66616041
BLAKE2b-256 4b7f614865fa059d4bab9705df9fcac6e29cf3a3f198797713ea16f9a68d0443

See more details on using hashes here.

File details

Details for the file SVGCompress-0.18-py2-none-any.whl.

File metadata

File hashes

Hashes for SVGCompress-0.18-py2-none-any.whl
Algorithm Hash digest
SHA256 1074fd30f032f1e48d00601060ae0927455e881721f4477c453c0c3c5e320737
MD5 f28c55db3eb7053fb23ac403b6d09728
BLAKE2b-256 78004f8495eb2f482e1c567f15251e0c47b79605f2feb15f6aa7ae160fed0d4b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page