Repository navigation
How to use #10
Description
Activity
After I use pip, how can I use this library to convert bitmaps to vectors
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-clipip install potrace-cliYou'd type
potracerthen 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 bitmapProgramatically 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.
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])
Reacted by katyachernovSee updated readme.
Is the color option missing?
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.