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

Overlay colored text (with an alpha layer) on the picture #113

Closed
AndrePScope opened this issue Feb 1, 2021 · 2 comments
Closed

Overlay colored text (with an alpha layer) on the picture #113

AndrePScope opened this issue Feb 1, 2021 · 2 comments
Labels
question Further information is requested

Comments

@AndrePScope
Copy link

AndrePScope commented Feb 1, 2021

Hi Net-Vips!

On our project, we decided to use your product instead of ImageMagick.
Net-vips is not documented well, and I'm having difficulties with implementation simple things.

I need to overlay colored text (with an alpha layer) on the picture.
I tried to do this as shown in one of the php-vips examples.

libvips/php-vips#94

		private void Test()
		{
			using var image = Image.NewFromFile("sample.jpg", access: Enums.Access.Sequential);
			using var text = Image.Text("Hello", "Arial 36px", width: image.Width -100);

			using var overlay = text
				.NewFromImage(Image.NewFromArray(new[,] { { 255, 0, 0 } }))
				.Copy(interpretation: Enums.Interpretation.Srgb)
				.Bandjoin(text);

			image.Composite(overlay, Enums.BlendMode.Over, 400, 400).
				WriteToFile("_result.jpg");
		}

But I get an exception on image.Composite line .

	Message: unable to call composite2
	         composite2: images do not have same numbers of bands

Please, correct me, what I am doing something wrong.

@kleisauke kleisauke added the question Further information is requested label Feb 1, 2021
@kleisauke
Copy link
Owner

Hi @AndrePScope,

libvips and this binding is rather low-level and requires some experience in image processing. I admit that the documentation could be a bit more extensive (see #99).

NewFromImage has a params overload where you can pass a single-dimensional array, for example:

@@ -4,7 +4,7 @@
 			using var text = Image.Text("Hello", "Arial 36px", width: image.Width -100);
 
 			using var overlay = text
-				.NewFromImage(Image.NewFromArray(new[,] { { 255, 0, 0 } }))
+				.NewFromImage(255, 0, 0)
 				.Copy(interpretation: Enums.Interpretation.Srgb)
 				.Bandjoin(text);
 

If you want a bit of opacity in the text, you could use:

@@ -3,10 +3,13 @@
 			using var image = Image.NewFromFile("sample.jpg", access: Enums.Access.Sequential);
 			using var text = Image.Text("Hello", "Arial 36px", width: image.Width -100);
 
+			// we'll use that as the alpha ... scale down to make it transparent
+			using var alpha = (text * 0.3).Cast(Enums.BandFormat.Uchar);
+
 			using var overlay = text
 				.NewFromImage(255, 0, 0)
 				.Copy(interpretation: Enums.Interpretation.Srgb)
-				.Bandjoin(text);
+				.Bandjoin(alpha);
 
 			image.Composite(overlay, Enums.BlendMode.Over, 400, 400).
 				WriteToFile("_result.jpg");

Also see libvips/pyvips#204 for other effects you could apply to text (such as shadows or neon effects).

@AndrePScope
Copy link
Author

Thank you @kleisauke!

Thank you for correcting the code and for the useful link!

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

No branches or pull requests

2 participants