Skip to main content

pixels2svg : Convert pixels to SVG square-based shapes.

Project description

pixels2svg v0.1.0

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

Medical image segmentation is another typical use case where one has pretty low-res images and sometimes needs to display them best for data visualization/presentation purposes. In this case, an SVG overlay with editable contours and opacity can be convenient.

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 pixel2svg package (GitHub fork available here), 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)
Output SVG image
Output SVG image with
customized contour style
(see examples/sword_outline.py)

Example 2 - brain scan + segmentation overlay

File Preview
Scan image (PNG)
Segmentation overlay (PNG)
Converted SVG segmentation overlay
Converted SVG overlayed on PNG scan
with customized opacity & contour style
(see examples/sword_outline.py)

Requirements

  • Python >= 3.7

Dependencies


Install

pip install pixels2svg

or directly from Github:

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

Usage examples

CLI

usage: python3 -m 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.1.0.tar.gz (20.6 kB view details)

Uploaded Source

File details

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

File metadata

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

File hashes

Hashes for pixels2svg-0.1.0.tar.gz
Algorithm Hash digest
SHA256 eefc589c178453a59f48cc18cb2c597d2f36ec0edcf5a974f138cff7619ae2f9
MD5 031b04eda9a6b57ae53ba7080f61f8c3
BLAKE2b-256 5cbff758290c041bb96c83d4ea5a7d76c9c18cb3a5cf9b0b13939da2394f64e8

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