Git Product home page Git Product logo

gotrace's Introduction

GoTrace

WARNING: This library is deprecated in favor of gotranspile/gotrace. It will not receive any updates.

Pure Go implementation of Potrace vectorization library. Supports simple SVG output generation.

Also includes cgo bindings for the original Potrace library.

Original image

Original

Vectorized image

Vectorized

Installation

go get github.com/dennwc/gotrace

Usage

Process image with an alpha channel, generate SVG:

bm := gotrace.NewBitmapFromImage(img, nil)
paths, _ := gotrace.Trace(bm, nil)
gotrace.WriteSvg(file, img.Bounds(), paths, "")

Custom threshold function:

bm := gotrace.NewBitmapFromImage(img, func(x, y int, c color.Color) bool {
    r, g, b, _ := c.RGBA()
    return r + g + b > 128
})
paths, _ := gotrace.Trace(bm, nil)

gotrace's People

Contributors

dennwc avatar rgritti avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

gotrace's Issues

License

Hey, and thanks for this great libray,

I'm maintaining Hugo and have been looking for a library like this to integrate into the image processing. But that would need to be open source. Could you add a suitable license to this repository?

Thanks!

Incompatibility with go modules

I have a compatibility problem with the go modules:

filters/svgtrace.go:12:2: unknown import path "github.com/dennwc/gotrace": unzip /home/xxx/go/pkg/mod/cache/download/github.com/dennwc/gotrace/@v/v0.0.0-20151111213338-0306f421d687.zip: malformed file path "aux.go": disallowed path element "aux.go"

as microsoft says:

  • Do not use the following reserved names for the name of a file:
    CON, PRN, AUX, NUL, COM1, COM2, COM3, COM4, COM5, COM6, COM7, COM8, COM9, LPT1, LPT2, LPT3, LPT4, LPT5, LPT6, LPT7, LPT8, and LPT9. Also avoid these names followed immediately by an extension; for example, NUL.txt is not recommended. For more information, see Namespaces.

it should be enough to rename the file aux.go

see here for further details

WriteSvg() does not process child elements

This is in fact same bug as reported in #4. It is caused when image has background (e.g. "white" in #4) as solid and "black" as transparent. Then trace code properly creates rectangle with holes as child elements. But as WriteSvg() function completely ignores child paths, result is SVG with just single black rectangle.

Same problem is valid even when background is transparent for objects with holes.
See this example (face is most visible missing feature):

Source

Output

Incorrect output: Jagged outlines and no holes

Hi, thanks a lot for your work on this package.

I tried to reproduce tracing the sample image from the readme.
The outlines look jagged and it seems that there are no holes at all in the image.

I tried using both gotrace.Trace and gotraceb.Trace
I'm on Ubuntu 22.04 using WLS2 from a Windows 11 host and I tried both go1.19.3 and go1.10.

When I use the potrace command line tool, the output looks fine.

package main

import (
	"fmt"
	"image"
	"image/color"
	_ "image/png"
	"os"

	"github.com/dennwc/gotrace"
	gotraceb "github.com/dennwc/gotrace/bindings"
)

func main() {
	imgPath := "/home/jakob/tmp/potrace/stanford-orig2.png"

	rdr, err := os.Open(imgPath)
	check(err)
	defer rdr.Close()

	img, _, err := image.Decode(rdr)
	check(err)

	thresholdFunc := func(_, _ int, cl color.Color) bool {
		r, g, b, _ := cl.RGBA()
		return r+g+b < 128
	}
	bm := gotrace.NewBitmapFromImage(img, thresholdFunc) 

	paths, err := gotrace.Trace(bm, nil)
	//paths, err := gotraceb.Trace(bm, nil)

	f, err := os.Create("/home/jakob/tmp/trace.svg")
	check(err)
	defer f.Close()

	gotrace.WriteSvg(f, image.Rect(0, 0, img.Bounds().Dx(), img.Bounds().Dy()), paths, "red")
}

func check(err error) {
	if err != nil {
		fmt.Println(err)
		panic(err)
	}
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.