How to blur a text?

Magick++ is an object-oriented C++ interface to ImageMagick. Use this forum to discuss, make suggestions about, or report bugs concerning Magick++.
Post Reply
Airon65
Posts: 75
Joined: 2017-02-26T02:19:38-07:00
Authentication code: 1151

How to blur a text?

Post 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" );
Airon65
Posts: 75
Joined: 2017-02-26T02:19:38-07:00
Authentication code: 1151

Re: How to blur a text?

Post 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" );
Airon65
Posts: 75
Joined: 2017-02-26T02:19:38-07:00
Authentication code: 1151

Re: How to blur a text?

Post 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 :(
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to blur a text?

Post 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/
Airon65
Posts: 75
Joined: 2017-02-26T02:19:38-07:00
Authentication code: 1151

Re: How to blur a text?

Post 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
Airon65
Posts: 75
Joined: 2017-02-26T02:19:38-07:00
Authentication code: 1151

Re: How to blur a text?

Post 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" );
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How to blur a text?

Post 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.
snibgo's IM pages: im.snibgo.com
Airon65
Posts: 75
Joined: 2017-02-26T02:19:38-07:00
Authentication code: 1151

Re: How to blur a text?

Post 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? :)
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How to blur a text?

Post by snibgo »

For most purposes, OverCompositeOp is the one we want.
snibgo's IM pages: im.snibgo.com
Airon65
Posts: 75
Joined: 2017-02-26T02:19:38-07:00
Authentication code: 1151

Re: How to blur a text?

Post by Airon65 »

Wow, it works! Thanks again! :)
Airon65
Posts: 75
Joined: 2017-02-26T02:19:38-07:00
Authentication code: 1151

Re: How to blur a text?

Post 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 :(
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How to blur a text?

Post 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.
snibgo's IM pages: im.snibgo.com
Airon65
Posts: 75
Joined: 2017-02-26T02:19:38-07:00
Authentication code: 1151

Re: How to blur a text?

Post 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!
Post Reply