disintegration / imaging

Imaging is a simple image processing package for Go
MIT License
5.22k stars 433 forks source link

Cannot apply transparency #148

Closed rafa-acioly closed 3 years ago

rafa-acioly commented 3 years ago

I'm trying to crop a image as a circle (https://github.com/disintegration/imaging/issues/72) and apply a color.Transparent, but the image is getting a white border

func makeCircleSmooth(src image.Image, factor float64) image.Image {
    d := src.Bounds().Dx()
    if src.Bounds().Dy() < d {
        d = src.Bounds().Dy()
    }
    dst := imaging.CropCenter(src, d, d)
    r := float64(d) / 2
    for x := 0; x < d; x++ {
        for y := 0; y < d; y++ {
            xf := float64(x)
            yf := float64(y)
            delta := math.Sqrt((xf-r)*(xf-r)+(yf-r)*(yf-r)) + factor - r
            switch {
            case delta > factor:
                dst.Set(x, y, color.Transparent)
            case delta > 0:
                m := 1 - delta/factor
                c := dst.NRGBAAt(x, y)
                c.A = uint8(float64(c.A) * m)
                dst.Set(x, y, c)
            }
        }
    }
    return dst
}

The background (PNG) colorful

The final result: final

I've tried with different pictures and I still got the same result

alhamsya commented 1 year ago

@rafa-acioly how do you solve this issue ?