Skip to main content

pixels2svg : Convert pixels to SVG square-based shapes.

Project description

pixels2svg v0.0.2

Convert pixels to SVG square-based shapes.

There are a few open source tools around that do bitmap -> vector tracing, for instance potrace which is the engine used by Inkscape and also has available python bindings (pypotrace).

But sometimes, what you want is the exact bitmap data, but as a set of SVG shapes, that you could for instance:

  • enlarge for big prints
  • use as input paths for a wood laser cutter
  • customize in a vector graphics software (edit opacity, contours, overlay on some background image etc.)

This package enables you to easily convert your bitmap image to its equivalent SVG representation, each path of the SVG being a polygon made of adjacent pixels of same color merged together.

It is inspired of Florian Berger's pixels2svg package, which is pretty old (python2) and doesn't merge adjacent pixels of same color, making the SVG pretty tedious to edit in vector graphics softwares.

Example 1 - pixel art

File Preview
Original PNG image
(32×32)
Original PNG image
(browser enlarged)
Converted SVG image
Converted SVG image - contours
(see examples/sword_outline.py)

Example 2 - brain scan + segmentation overlay

File Preview
Scan image
Segmentation PNG overlay
Converted SVG segmentation overlay
Blended segmentation SVG + scan
(see examples/sword_outline.py)

Requirements

  • Python >= 3.0

Dependencies


Install

pip install pixels2svg

or directly from Github:

pip install git+git://github.com/ValentinFrancois/pixels2svg#egg=pixels2svg

Usage examples

CLI

usage: pixels2svg [-h] [--output path] [--no_group_by_color] [--no_pretty] path

pixels2svg CLI

positional arguments:
  path                  input path of the bitmap image. If not passed, will print the output in the terminal.

optional arguments:
  -h, --help            show this help message and exit
  --output path, -o path
                        output path of the bitmap image
  --no_group_by_color   do not group shapes of same color together inside <g> tags
  --no_pretty           do not pretty-write the SVG code

In Python

Simple usage

from pixels2svg import pixels2svg

pixels2svg('input.png', 'output.svg')

pixels2svg() accepts a few optional arguments that should help you integrate it best within your codebase:

Parameters

  • input_path : str
    Path of the input bitmap image

  • output_path : Optional[str]
    Path of the output SVG image (optional). If passed, the function will return None. If not passed, the function will return the SVG data as a str or a Drawing depending on the as_string parameter.

  • group_by_color : bool
    If True (default), group same-color shapes under SVG elements.

  • as_string : bool
    If True and no output_path is passed, return a str representing the SVG data.

  • pretty : bool
    If True (default), output SVG code is pretty-printed.

Returns

  • Optional[Union[svg.Drawing, str]]
    Depends on the output_path and as_string parameters

Advanced usage

By setting output_path=None and as_string=False, you can get an object inheriting svgwrite.Drawing, with the additional useful methods:

  • def save_to_path(self, path: str, pretty: bool = False)
  • def to_string(self, pretty: bool = False) ‑> str

This gives you access to the svgwrite API to modify the output programmatically.

See advanced examples in examples.


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

pixels2svg-0.0.2.tar.gz (20.2 kB view hashes)

Uploaded Source

Supported by

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