govips
govips copied to clipboard
How to create a transparent canvas?
Im trying to create a empty transparent canvas that i can add images on.
right now i am resorting to
c := image.NewNRGBA(rectangle)
buf := &bytes.Buffer{}
if err := png.Encode(buf, c); err != nil {
panic(err)
}
canvas, err := vips.NewImageFromBuffer(buf.Bytes())
if err != nil {
panic(err)
}
which feels toaly backwards.
I have tried using vips.Black() but it only gives me a black image and when i try to draw using img.Insert(), the whole image is still just black
if i do canvas := &vips.ImageRef{} i cannot propagate all fields needed for it to work
Any smart things i've missed in the lib?
Cheers and thanks!
Found this way to do it as well, but still feels very hacky :/
// bytes for a 1x1 transparent png umage
var tmpl = []byte{0x89, 0x50, 0x4E, 0x47, 0xD, 0xA, 0x1A, 0xA, 0x0, 0x0, 0x0, 0xD, 0x49, 0x48, 0x44, 0x52, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0x1, 0x8, 0x6, 0x0, 0x0, 0x0, 0x1F, 0x15, 0xC4, 0x89, 0x0, 0x0, 0x0, 0x11, 0x49, 0x44, 0x41, 0x54, 0x78, 0x9C, 0x62, 0x62, 0x60, 0x60, 0x60, 0x0, 0x4, 0x0, 0x0, 0xFF, 0xFF, 0x0, 0xF, 0x0, 0x3, 0xFE, 0x8F, 0xEB, 0xCF, 0x0, 0x0, 0x0, 0x0, 0x49, 0x45, 0x4E, 0x44, 0xAE, 0x42, 0x60, 0x82}
imageCount := len(images)
canvas, err := vips.NewImageFromBuffer(tmpl)
if err != nil {
panic(err)
}
width := rectangle.Dx()
height := rectangle.Dy()
if err := canvas.ResizeWithVScale(float64(width), float64(height), vips.KernelAuto); err != nil {
panic(err)
}
would be nice if i just could createa image filled with alpha direcltly instead of drawing a black one, adding alpha and then filling it with a big rectangle
func newCanvas(width, height int) *vips.ImageRef {
canvas, err := vips.Black(width, height)
if err != nil {
panic(err)
}
if err := canvas.AddAlpha(); err != nil {
panic(err)
}
if err := canvas.ToColorSpace(vips.InterpretationSRGB); err != nil {
panic(err)
}
if err := canvas.DrawRect(vips.ColorRGBA{R: 0, G: 0, B: 0, A: 0}, 0, 0, width, height, true); err != nil {
panic(err)
}
return canvas
}