fogleman / gg

Go Graphics - 2D rendering in Go with a simple API.
https://godoc.org/github.com/fogleman/gg
MIT License
4.4k stars 358 forks source link

Cropping according to a custom shape, like a polygon or hexagon #155

Closed darklight147 closed 2 years ago

darklight147 commented 2 years ago

How do i use the Crop function to crop a custom shape, instead of rectangle

darklight147 commented 2 years ago

My solutions was creating a mask from a context, thanks guys great library.

Jefry-nolastname commented 1 year ago

can you please share how you cropped it?

darklight147 commented 1 year ago

Here is an example code @Jefry-nolastname :

var frameMask image.Image

frameMask, _ = gg.LoadPNG("./assets/mask.png")

dc := gg.NewContext(100, 100)

dc2 := gg.NewContextForImage(frameMask)

mask := dc2.AsMask()
dc.SetMask(mask)
dc.DrawImage(YOUR_IMAGE, 0, 0)
dc.ResetClip()

// Here you should have the image cropped in the `dc` context
Jefry-nolastname commented 1 year ago

thankyou so much