Skip to content

How to use #10

Description

@1JayPeng
No description provided.

Activity

  1. changed the title [-]use[/-] [+]How to use[/+] on Jun 14, 2023
  2. 1JayPeng commented on Jun 14, 2023

    @1JayPeng
    Author

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

  3. tatarize commented on Jun 14, 2023

    @tatarize
    Owner

    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
    
  4. Dwiga commented on Nov 8, 2023

    @Dwiga

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

  5. tatarize commented on Nov 29, 2023

    @tatarize
    Owner

    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.

  6. tatarize commented on Nov 29, 2023

    @tatarize
    Owner
    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])
  7. added a commit that references this issue on Nov 29, 2023
  8. tatarize commented on Nov 29, 2023

    @tatarize
    Owner

    See updated readme.

  9. AeroDEmi commented on Mar 21, 2024

    @AeroDEmi

    Is the color option missing?

  10. tatarize commented on Mar 22, 2024

    @tatarize
    Owner

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions