Skip to main content

pixels2svg : Convert pixels to SVG square-based shapes.

Project description

pixels2svg v0.0.1

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.1.tar.gz (20.1 kB view details)

Uploaded Source

File details

Details for the file pixels2svg-0.0.1.tar.gz.

File metadata

  • Download URL: pixels2svg-0.0.1.tar.gz
  • Upload date:
  • Size: 20.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.8.10

File hashes

Hashes for pixels2svg-0.0.1.tar.gz
Algorithm Hash digest
SHA256 205c6a71b4b7cfa858b0d0ebd297e99c466558d1c56ea21526161ae9fe474007
MD5 119a585304245ea0b3eb704ee71498ec
BLAKE2b-256 8e69ba9b903fc5a58699a77bc7ee6fbd0ee3bd8fd21dffd7786c35270c306e9f

See more details on using hashes here.

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