Letter Blur

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
The Little Guy
Posts: 9
Joined: 2011-03-19T07:56:19-07:00
Authentication code: 8675308

Letter Blur

Post by The Little Guy »

Hello! This is my first post! I am not new to IM but I also am not good at it either, so thank you everyone for all the help I will need!

I have the following code:

Code: Select all

shell_exec("convert -size {$this->width}x{$this->height} xc:'$this->bgColor' $file");
for(...){
      shell_exec("mogrify -blur $blur -font $font -fill '$color' -pointsize $size -draw \"text $txS,$tyP '".$text{$i}."'\" $file");
}
The loop runs for every letter I have to make a style for that letter (to build a CAPTCHA) setting all the properties, but the first letter is blurred the most and the last letter is blurred the least or not at all. Is there any way I can set a blur for each letter instead of blurring whole image every time I go through the loop?
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Letter Blur

Post by Bonzo »

Why not generate the whole image then blur it ?

Code: Select all

exec("convert -size 200x100 xc:lightblue -gravity west -font Arial -fill black -pointsize 30 -annotate +0+0 'A' -font Verdana -fill red  -pointsize 30 -annotate +50+0 'B' -font Arial -fill blue -pointsize 30 -annotate +100+0 'C' -blur 5 blured.jpg" ); 
The Little Guy
Posts: 9
Joined: 2011-03-19T07:56:19-07:00
Authentication code: 8675308

Re: Letter Blur

Post by The Little Guy »

Oh, I forgot to say this, but the reason I didn't do that is because each letter will have a different blur amount, either no blur, or a blur between 5 and 10.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Letter Blur

Post by fmw42 »

The loop runs for every letter I have to make a style for that letter (to build a CAPTCHA) setting all the properties, but the first letter is blurred the most and the last letter is blurred the least or not at all. Is there any way I can set a blur for each letter instead of blurring whole image every time I go through the loop?
According to your code, the blur should be the same for all letters.

What version of IM and platform are you using? Perhaps you need to upgrade IM.
The Little Guy
Posts: 9
Joined: 2011-03-19T07:56:19-07:00
Authentication code: 8675308

Re: Letter Blur

Post by The Little Guy »

The loop is fairly big so I didn't post it all, but I guess it will help any:

Code: Select all

for($i=0;$i<$this->length;$i++){
	$txS += $size;
	$tyP = round(rand(20, $this->height));
	if($this->font_family==FONT_RANDOM){
		$fonts = $this->font_list();
	}else{
		$fonts = explode(',', $this->font_family);
	}
	$fontid = array_rand($fonts);
	$font = $fonts[$fontid];
	if($this->color==COLOR_RANDOM){
		$color = $this->randColor();
	}else{
		$colors = explode(',', $this->color);
		$colorid = array_rand($colors);
		$color = $colors[$colorid];
	}
	$size = round(rand(12, 30));
	$blur = ((bool)round(rand(0, 1))) ? round(rand(5, 10)) : 0 ;
	shell_exec($opt = "mogrify -blur $blur -font $font -fill '$color' -pointsize $size -draw \"text $txS,$tyP '".$text{$i}."'\" $file");
}
Linux:
Version: ImageMagick 6.3.7 11/16/10 Q16 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2008 ImageMagick Studio LLC
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Letter Blur

Post by fmw42 »

version 6.3.7 is about 300 versions old. perhaps you ought to upgrade, if you can. Though perhaps the error is in your script?
The Little Guy
Posts: 9
Joined: 2011-03-19T07:56:19-07:00
Authentication code: 8675308

Re: Letter Blur

Post by The Little Guy »

I am fairly sure its not the script.

What I think is happening, is (in Photoshop terms):
- it creates an image
- it adds a letter to the image
- it flattens the image, then blurs the first letter
- it adds the next letter, flattens the image, then blurs the letters again

and it keeps doing this until all the letters have been added.

to me, that is what it sounds like is happening.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Letter Blur

Post by fmw42 »

If you are adding a letter to the right side, then the last image should be less blurred.

(Sorry I had it backwards before. But el_supremo has it correct below)
Last edited by fmw42 on 2011-03-19T21:40:50-07:00, edited 1 time in total.
The Little Guy
Posts: 9
Joined: 2011-03-19T07:56:19-07:00
Authentication code: 8675308

Re: Letter Blur

Post by The Little Guy »

Each letter is blurred a different amount, but the left side one is always the most. There should be some in there that are not blurred at all other than the last letter, but that never happens.

Image
el_supremo
Posts: 1015
Joined: 2005-03-21T21:16:57-07:00

Re: Letter Blur

Post by el_supremo »

Each letter is blurred a different amount, but the left side one is always the most
Which is exactly how you wrote the code. As a simple example what your script is doing is: start with letter A and blur it. Now append a B to get AB and then blur that. Now append C to get ABC and then blur that and so on. The letter on the right will be the least blurred.
As you said in your first post, you need to create each letter individually, blur them and then assemble them into the final image.
Instead of starting with a widthxheight canvas, start with one that is 1xheight. Then in the loop create each blurred character (using "convert"), read in the canvas again, append the two together and then overwrite this on the canvas.
The initial canvas will be created with:

Code: Select all

shell_exec("convert -size 1x{$this->height} xc:'$this->bgColor' $file");
and replace the mogrify command with a convert command that looks something like this (note that the -blur has moved after the -draw):

Code: Select all

   shell_exec($opt = "convert -size {$this->width}x{$this->height} xc:'$this->bgColor' -font $font -fill '$color' -pointsize $size -draw \"text $txS,$tyP '".$text{$i}."'\" -blur $blur +repage -append $file $file");
Pete
Sorry, my ISP shutdown all personal webspace so my MagickWand Examples in C is offline.
See my message in this topic for a link to a zip of all the files.
el_supremo
Posts: 1015
Joined: 2005-03-21T21:16:57-07:00

Re: Letter Blur

Post by el_supremo »

Sorry, some corrections and modifications in the convert command.
- the width of the image in the -size should be $this->width divided by $this->length
- the coordinate in the text subcommand is wrong for this method. It's probably better to use 0,0 with a gravity of center (or perhaps west)
- your -blur argument would be better if it were 0x$blur which varies the sigma rather than the radius. You may find that your current random range for blur is too much.
- I had -append when it should be +append and it was in the wrong place.
Something like this should work but I don't know PHP syntax well enough to be sure of it (and how do you continue long lines in PHP?).

Code: Select all

$canvas_width = $this->width / $this->length;
shell_exec($opt = "convert -size {$canvas_width}x{$this->height} xc:'$this->bgColor' -font $font -fill '$color' -pointsize $size -draw \"gravity center text 0,0'".$text{$i}."'\" -blur 0x$blur $file +append $file");
Pete
Sorry, my ISP shutdown all personal webspace so my MagickWand Examples in C is offline.
See my message in this topic for a link to a zip of all the files.
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Letter Blur

Post by Bonzo »

I would write the code like this which allows for long lines:

Code: Select all

$opt = "-size {$canvas_width}x{$this->height} xc:'$this->bgColor' ".
" -font $font -fill $color -pointsize $size -draw \"gravity center text 0,0'".$text{$i}."'\" ".
" -blur 0x$blur $file +append ";

shell_exec("convert $opt $file");

The Little Guy
Posts: 9
Joined: 2011-03-19T07:56:19-07:00
Authentication code: 8675308

Re: Letter Blur

Post by The Little Guy »

el_supremo wrote:

Code: Select all

$canvas_width = $this->width / $this->length;
shell_exec($opt = "convert -size {$canvas_width}x{$this->height} xc:'$this->bgColor' -font $font -fill '$color' -pointsize $size -draw \"gravity center text 0,0'".$text{$i}."'\" -blur 0x$blur $file +append $file");
Sweet man that works! Now I just need to add other random effects!

el_supremo wrote:how do you continue long lines in PHP?

Code: Select all

<?php
//With an echo:
echo "this is line 1
this is line 2
this is line 3";

// With a variable
$variable = "this is line 1
this is line 2
this is line 3";
?>
@Bonzo
You don't need to end the line and concatenate it with a new line.
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Letter Blur

Post by Bonzo »

You don't need to end the line and concatenate it with a new line.
Thanks I did wonder if that could be done but have never tested it.
The Little Guy
Posts: 9
Joined: 2011-03-19T07:56:19-07:00
Authentication code: 8675308

Re: Letter Blur

Post by The Little Guy »

As I have been working with this, I have noticed that the image changes sizes.

How can I force it to be a particular width without cropping the image?

for example I tell the image to be 300px wide, when it is displayed it turns out to be 261px wide or something smaller than or equal to 300.
Post Reply