potrace icon indicating copy to clipboard operation
potrace copied to clipboard

How to use

Open 1JayPeng opened this issue 1 year ago • 8 comments

1JayPeng avatar Jun 14 '23 07:06 1JayPeng

After I use pip, how can I use this library to convert bitmaps to vectors

1JayPeng avatar Jun 14 '23 07:06 1JayPeng

If you want to perform that action directly from command line you would need to install potrace-cli since that has the command line bindings. https://github.com/tatarize/potrace-cli

pip install potrace-cli

You'd type potracer then from the command line if you wanted to access the commandline menu:

usage: potracer [-h] [-v] [-l] [-o OUTPUT] [-b {svg,jagged-svg}]
               [-z {black,white,left,right,minority,majority,random}]
               [-t TURDSIZE] [-a ALPHAMAX] [-n] [-O OPTTOLERANCE] [-C COLOR]
               [-i] [-k BLACKLEVEL] [-s SCALE] [-1]
               [filename]

positional arguments:
  filename              an input file

optional arguments:
  -h, --help            show this help message and exit
  -v, --version         prints version info and exit
  -l, --license         prints license info and exit
  -o OUTPUT, --output OUTPUT
                        write all output to this file
  -b {svg,jagged-svg}, --backend {svg,jagged-svg}
                        select backend by name
  -z {black,white,left,right,minority,majority,random}, --turnpolicy {black,white,left,right,minority,majority,random}
                        how to resolve ambiguities in path decomposition
  -t TURDSIZE, --turdsize TURDSIZE
                        suppress speckles of up to this size (default 2)
  -a ALPHAMAX, --alphamax ALPHAMAX
                        corner threshold parameter
  -n, --longcurve       turn off curve optimization
  -O OPTTOLERANCE, --opttolerance OPTTOLERANCE
                        curve optimization tolerance
  -C COLOR, --color COLOR
                        set foreground color (default Black)
  -i, --invert          invert bitmap
  -k BLACKLEVEL, --blacklevel BLACKLEVEL
                        invert bitmap

tatarize avatar Jun 14 '23 11:06 tatarize

@1JayPeng did you find your solution? is it only work on cli? can I use it programmatically? @tatarize

Dwiga avatar Nov 08 '23 09:11 Dwiga

Programatically is the expected way to use the library. But, yeah, the documentation and example code to do that is decidedly quite lacking. I'll add some.

tatarize avatar Nov 29 '23 20:11 tatarize

import sys
from PIL import Image
from potrace import Bitmap, POTRACE_TURNPOLICY_MINORITY  # `potracer` library


def file_to_svg(filename: str):
    try:

        image = Image.open(filename)
    except IOError:
        print("Image (%s) could not be loaded." % filename)
        return
    bm = Bitmap(image, blacklevel=0.5)
    # bm.invert()
    plist = bm.trace(
        turdsize=2,
        turnpolicy=POTRACE_TURNPOLICY_MINORITY,
        alphamax=1,
        opticurve=False,
        opttolerance=0.2,
    )
    with open(f"{filename}.svg", "w") as fp:
        fp.write(
            f'''<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="{image.width}" height="{image.height}" viewBox="0 0 {image.width} {image.height}">''')
        parts = []
        for curve in plist:
            fs = curve.start_point
            parts.append(f"M{fs.x},{fs.y}")
            for segment in curve.segments:
                if segment.is_corner:
                    a = segment.c
                    b = segment.end_point
                    parts.append(f"L{a.x},{a.y}L{b.x},{b.y}")
                else:
                    a = segment.c1
                    b = segment.c2
                    c = segment.end_point
                    parts.append(f"C{a.x},{a.y} {b.x},{b.y} {c.x},{c.y}")
            parts.append("z")
        fp.write(f'<path stroke="none" fill="black" fill-rule="evenodd" d="{"".join(parts)}"/>')
        fp.write("</svg>")


if __name__ == '__main__':
    file_to_svg(sys.argv[1])

tatarize avatar Nov 29 '23 21:11 tatarize

See updated readme.

tatarize avatar Nov 29 '23 21:11 tatarize

Is the color option missing?

AeroDEmi avatar Mar 21 '24 18:03 AeroDEmi

Is the color option missing?

Yes and no. Color is typically done by isolating each color into black and white, see descriptions of how inkscape uses the algo. The original, and this code only really works in black and white. Its not strictly part of potrace.

tatarize avatar Mar 22 '24 11:03 tatarize