Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to create a transparent canvas? #306

Open
roffe opened this issue Sep 13, 2022 · 3 comments
Open

How to create a transparent canvas? #306

roffe opened this issue Sep 13, 2022 · 3 comments

Comments

@roffe
Copy link
Contributor

roffe commented Sep 13, 2022

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!

@roffe
Copy link
Contributor Author

roffe commented Sep 13, 2022

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)
	}

@roffe
Copy link
Contributor Author

roffe commented Sep 13, 2022

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
}

@roffe
Copy link
Contributor Author

roffe commented Nov 9, 2022

func newCanvas(width, height int) (*vips.ImageRef, error) {
	canvas, err := vips.Black(1, 1)
	if err != nil {
		return nil, err
	}
	if err := canvas.ToColorSpace(vips.InterpretationSRGB); err != nil {
		return nil, err
	}
	if err := canvas.AddAlpha(); err != nil {
		return nil, err
	}
	if err := canvas.Invert(); err != nil {
		return nil, err
	}
	if err := canvas.Embed(0, 0, width, height, vips.ExtendCopy); err != nil {
		return nil, err
	}
	return canvas, nil
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant