Page 1 of 1

How to blur a text?

Posted: 2017-03-28T11:03:57-07:00
by Airon65
This my failed attempt to blur "Some text 2" only. I need to apply blur effect only for the "Some text 2". As you see my current code blurs all the image. How can I apply blur only for the "Some text 2"?

Code: Select all

Magick::Image image( Magick::Geometry( 800, 600 ), Magick::Color( "white" ) );

image.font( "verdana" );
image.fontPointsize( 100 );
image.fillColor( Magick::Color( "maroon" ) );
image.strokeColor( Magick::Color( "red" ) );
image.strokeWidth( 2 );

image.draw( Magick::DrawableText( 0, 175,  "Some text 1" ) );

image.draw( Magick::DrawableText( 0, 275,  "Some text 2" ) );
image.blur( 30.0f, 10.0f );

image.write( "images/image.png" );

Re: How to blur a text?

Posted: 2017-03-28T12:19:55-07:00
by Airon65
I can try to put one image over another but I don't know how to create an empty second image to put it over the first one:

Code: Select all

Magick::Image image( Magick::Geometry( 800, 600 ), Magick::Color( "white" ) );
image.font( "verdana" );
image.fontPointsize( 100 );
image.fillColor( Magick::Color( "maroon" ) );
image.strokeColor( Magick::Color( "red" ) );
image.strokeWidth( 2 );

image.draw( Magick::DrawableText( 0, 175,  "Some text 1" ) );
image.blur( 30.0f, 10.0f );

Magick::Image image2( Magick::Geometry( 800, 600 ), Magick::Color( "white" ) );
image2.font( "verdana" );
image2.fontPointsize( 100 );
image2.fillColor( Magick::Color( "maroon" ) );
image2.strokeColor( Magick::Color( "red" ) );
image2.strokeWidth( 2 );
image2.draw( Magick::DrawableText( 0, 275,  "Some text 2" ) );

image.draw( Magick::DrawableCompositeImage( 0, 0, image2 ) );

image.write( "images/image.png" );

Re: How to blur a text?

Posted: 2017-03-28T14:35:09-07:00
by Airon65
I've tried to go this way:

Code: Select all

	Magick::Image image2( Magick::Geometry( 800, 600 ), Magick::Color( 0, 0, 0, 0 ) );
	image2.font( "verdana" );
	image2.fontPointsize( 100 );
	image2.fillColor( Magick::Color( "maroon" ) );
	image2.strokeColor( Magick::Color( "red" ) );
	image2.strokeWidth( 2 );
	image2.draw( Magick::DrawableText( 0, 275,  "Some text 2" ) );

	image.draw( Magick::DrawableCompositeImage( 0, 0, image2 ) );
but now I see only the last image... but I need all images over the only one image. I've tried also this:

Code: Select all

	//image2.artifact( "compose:args", "50,50" );
	//image.composite( image2, 0, 0, Magick::BlendCompositeOp ); // походу Бленда уже нет, попробовать: AddCompositeOp или OverCompositeOp,
	image.composite( image2, 0, 0, Magick::AddCompositeOp );
It's not: image1 + image2 + image3 :(

Re: How to blur a text?

Posted: 2017-03-28T15:24:48-07:00
by fmw42
Please provide example images and an example output or a description of what you want done to the images to produce the output.

Please, always provide your IM version and platform when asking questions, since syntax may differ. Also provide your exact command line and if possible your images.

See the top-most post in this forum "IMPORTANT: Please Read This FIRST Before Posting" at viewtopic.php?f=1&t=9620

For novices, see

viewtopic.php?f=1&t=9620
http://www.imagemagick.org/script/comma ... essing.php
http://www.imagemagick.org/Usage/reference.html
http://www.imagemagick.org/Usage/

Re: How to blur a text?

Posted: 2017-03-28T16:46:14-07:00
by Airon65
Thanks for the answer!

Code: Select all

Please provide example images and an example output or a description of what you want done to the images to produce the output. 
There are no images. Just a few text labels. I want to get one image with 3 text labels: one of that label should be blurred... But I can't just use: img.blur( ... ) because it'll apply to whole image but I need to get blurred just one label only :)

Another sample where I need to use is to apply some text-label watermark for some images and I want to blur my text-label-watermark a little but I can't do it because the whole image will be blurred not the only watermark :)

ImageMagick 7.0.5-2 Q16 x86_64 2017-03-11 / OS X 10.12.3

Re: How to blur a text?

Posted: 2017-03-28T17:04:04-07:00
by Airon65
In other words I have a code:

Code: Select all

Magick::Image image( "any-image.png" );

image.font( "verdana" );
image.fontPointsize( 100 );
image.fillColor( Magick::Color( "maroon" ) );
image.strokeColor( Magick::Color( "red" ) );
image.strokeWidth( 2 );

image.draw( Magick::DrawableText( 0, 175,  "Some text 1" ) );

image.draw( Magick::DrawableText( 0, 275,  "Some text 2" ) ); //<---- I NEED TO GET BLURRED only this text not the whole image!

image.write( "output.png" );

Re: How to blur a text?

Posted: 2017-03-28T17:36:23-07:00
by snibgo
Airon wrote:As you see my current code blurs all the image.
Yes. IM operators process the entire image. As you realise, you should create the text as a separate image, blur it, and composite that over another image.
Airon wrote:I need to apply blur effect only for the "Some text 2".
But in your second attempt, you draw "Some text 1" into image, and blur that. Then you create image2, draw "Some text 2" on that, you don't blur it, and draw it over image.

Re: How to blur a text?

Posted: 2017-03-28T18:22:58-07:00
by Airon65
you should create the text as a separate image, blur it, and composite that over another image.
Yeah, that's what I need. But I can't do it :) If I go that way:

Code: Select all

Magick::Image image( Magick::Geometry( 800, 600 ), Magick::Color( "white" ) );

image.font( "verdana" );
image.fontPointsize( 100 );
image.fillColor( Magick::Color( "maroon" ) );
image.strokeColor( Magick::Color( "red" ) );
image.strokeWidth( 2 );
image.draw( Magick::DrawableText( 0, 175,  "Some text 1" ) );

Magick::Image image2( Magick::Geometry( 800, 600 ), Magick::Color( 0, 0, 0, 0 ) );
image2.font( "verdana" );
image2.fontPointsize( 100 );
image2.fillColor( Magick::Color( "yellow" ) );
image2.strokeColor( Magick::Color( "red" ) );
image2.strokeWidth( 2 );
image2.draw( Magick::DrawableText( 0, 200,  "Some text 2" ) );

image2.artifact( "compose:args", "50,50" );
image.composite( image2, 0, 0, Magick::BlendCompositeOp ); 
I get this output: Image

Where's the bounds of the yellow label? :) Ok, but if I go that way:

Code: Select all

image.composite( image2, 0, 0, Magick::AddCompositeOp );
I get similar result. I just need two labels one over another... without blending it... just... copy one over another :) Is it possible? :)

Re: How to blur a text?

Posted: 2017-03-28T18:34:10-07:00
by snibgo
For most purposes, OverCompositeOp is the one we want.

Re: How to blur a text?

Posted: 2017-03-28T18:44:44-07:00
by Airon65
Wow, it works! Thanks again! :)

Re: How to blur a text?

Posted: 2017-03-29T03:07:25-07:00
by Airon65
you should create the text as a separate image, blur it, and composite that over another image.
I have some problem with the performance of this approach. It seems that ImageMagick blurs all pixels of big image not only the pixels of my text label. For example if we have:

Code: Select all

Magick::Image image2( Magick::Geometry( 4096, 3072 ), Magick::Color( 0, 0, 0, 0 ) ); // 4k resolution
image2.font( "verdana" );
image2.fontPointsize( 20 ); // <-- this is really small text label!
image2.fillColor( Magick::Color( "yellow" ) );
image2.strokeColor( Magick::Color( "red" ) );
image2.strokeWidth( 2 );
image2.draw( Magick::DrawableText( 0, 200,  "Current time is: 12:57" ) );

image2.blur( 10.0f, 10.0f ); // <-- maybe I need this for the shadow behind the text label with automatic generated text in it
I guess It wastes to much time for blurring those transparent empty pixels around the text... isn't it? Or it doesn't blur transparent pixels? It's not good for performance if I have a bunch of thousand such images to process :(

Re: How to blur a text?

Posted: 2017-03-29T03:25:10-07:00
by snibgo
Your image2 is 4096x3072 pixels. Yes, most of those will be transparent, and blurring those takes time and has no effect.

So you might create a smaller image.

Or trim the image after adding the text, then "extent" or border the image somewhat to allow for spreading by the blur.

EDIT: Minor typo fixed.

Re: How to blur a text?

Posted: 2017-03-29T03:56:20-07:00
by Airon65
Or trim the image after adding the text, then "extent" or border the image the image somewhat to allow for spreading by the blur.
I like it. Thanks in advance!