Merging 2 gifs, one is animated and transparent

Questions and postings pertaining to the development of ImageMagick, feature enhancements, and ImageMagick internals. ImageMagick source code and algorithms are discussed here. Usage questions which are too arcane for the normal user list should also be posted here.
peixe01
Posts: 7
Joined: 2011-07-11T13:41:39-07:00
Authentication code: 8675308

Merging 2 gifs, one is animated and transparent

Post by peixe01 » 2011-07-11T14:03:30-07:00

for the life of me i cant get this done... i am trying to create an animated captcha. I have built the background image with the text and have the animated image to place on top but i cant seem to merge them right. What i have right now is the first frame with the text merged with the animated image and then the text goes away and i only get the animated part.

wheelp.gif is already saved in disk.

Code: Select all

imagegif($image_2,"/home/**/public_html/captcha/ver2/text.gif");

$path = "/home/**/public_html/captcha/ver2";

$cmd = "convert $path/text.gif $path/wheel.gif -loop 0 $path/captcha.gif"; 

exec("$cmd 2>&1", $err); 
Please help, what am i missing :(
Last edited by peixe01 on 2011-07-13T03:11:13-07:00, edited 1 time in total.

User avatar
fmw42
Posts: 25654
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Merging 2 gifs, one is animated and transparent

Post by fmw42 » 2011-07-11T15:09:57-07:00

see the -draw approach at http://www.imagemagick.org/Usage/anim_mods/#compose or the null: -layers composite approach at http://www.imagemagick.org/Usage/anim_mods/#composite

an example of this was discussed recently on the User's forum. see viewtopic.php?f=1&t=19000

peixe01
Posts: 7
Joined: 2011-07-11T13:41:39-07:00
Authentication code: 8675308

Re: Merging 2 gifs, one is animated and transparent

Post by peixe01 » 2011-07-11T16:07:21-07:00

thanks fred, thast just so much information i am not sure what to grab, all my tests fail, i though this would be somewhat easy, why would -draw be needed? both images have the same size. -layers composite also not sure, there is only 1 layer with transparency, the foreground image.

User avatar
fmw42
Posts: 25654
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Merging 2 gifs, one is animated and transparent

Post by fmw42 » 2011-07-11T16:18:13-07:00

peixe01 wrote:thanks fred, thast just so much information i am not sure what to grab, all my tests fail, i though this would be somewhat easy, why would -draw be needed? both images have the same size. -layers composite also not sure, there is only 1 layer with transparency, the foreground image.
can you post a link to your background image and animation image? if not tell me what the names are an I will give you the command line. is text.gif the background and wheel.gif the animation or the other way around.

One way is:

convert animation.gif -coalesce \
-draw 'image DstOver 0,0 0,0 "background.gif"' \
-layers Optimize new_animation.gif

The other way is:

convert background.gif null: \( animation.gif -coalesce \) \
-layers Composite \
-layers Optimize new_animation.gif
Last edited by fmw42 on 2011-07-11T17:10:14-07:00, edited 1 time in total.

peixe01
Posts: 7
Joined: 2011-07-11T13:41:39-07:00
Authentication code: 8675308

Re: Merging 2 gifs, one is animated and transparent

Post by peixe01 » 2011-07-11T16:29:28-07:00

background: http://www.bulletdrive.com/captcha/ver2/text.gif
animation on op of it: http://www.bulletdrive.com/captcha/ver2/wheel.gif

final image: http://www.bulletdrive.com/captcha/ver2/captcha.gif

right now i am getting from your tip: convert: Non-conforming drawing primitive definition `home/bulletdr/public_html/captcha/ver2/text.gif'.

my script is on http://www.bulletdrive.com/captcha/ver2/go.php


thanks again.

User avatar
fmw42
Posts: 25654
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Merging 2 gifs, one is animated and transparent

Post by fmw42 » 2011-07-11T17:16:02-07:00

Both of these work fine for me on IM 6.7.1.0 Q16 Mac OSX Tiger.


convert wheel.gif -coalesce \
-draw 'image DstOver 0,0 0,0 "text.gif"' \
-layers Optimize tmp1.gif

convert text.gif null: \( wheel.gif -coalesce \) \
-layers Composite \
-layers Optimize tmp2.gif

You can try either without the -coalesce as in your case it does not matter. The -draw method should be more backward compatible with older versions of IM.


What version of IM are you using and what platform? It is possible that your version of IM is too old. Can you run this in the command line rather than via PHP. Send me your full php script.

Note that I corrected a cut and paste error in the command using -draw to remove the colon I had accidentally left in "text.gif:". See the corrected usage above here in the first example.

peixe01
Posts: 7
Joined: 2011-07-11T13:41:39-07:00
Authentication code: 8675308

Re: Merging 2 gifs, one is animated and transparent

Post by peixe01 » 2011-07-12T14:17:31-07:00

ok it works mate, thanks a bunch.

Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Merging 2 gifs, one is animated and transparent

Post by Bonzo » 2011-07-12T14:31:33-07:00

Thats a novel approch for a CAPTCA are you generating the text for each user or using the same all the time?

peixe01
Posts: 7
Joined: 2011-07-11T13:41:39-07:00
Authentication code: 8675308

Re: Merging 2 gifs, one is animated and transparent

Post by peixe01 » 2011-07-12T14:32:42-07:00

one thing i notice is that the wheel loses quality when running the command via PHP, does that make any sense?


check the final result: http://www.bulletdrive.com/captcha/ver2/go.php
(refresh gives new captcha)

i save the captcha for 5 minutes and its the same for every user, then refresh it.

User avatar
fmw42
Posts: 25654
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Merging 2 gifs, one is animated and transparent

Post by fmw42 » 2011-07-12T16:58:10-07:00

peixe01 wrote:one thing i notice is that the wheel loses quality when running the command via PHP, does that make any sense?


check the final result: http://www.bulletdrive.com/captcha/ver2/go.php
(refresh gives new captcha)

i save the captcha for 5 minutes and its the same for every user, then refresh it.

Can you post your php file so that one can see the code and not just the image display?

Are you using the same version of IM with PHP as with the command line?

User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Merging 2 gifs, one is animated and transparent

Post by anthony » 2011-07-13T20:09:39-07:00

As a catchpa it is easily defeated. Just look for the pixels that don't change much and you can remove the animated overlay!
A little morphology and the text becomes clear, ready for OCP to convert to plain text.

A animated distortion of the text as well may help so as to remove any 'static' parts to the image.

However in general animations provide far more information for the catchpa solvers to use in breaking it.
Of course it can also provide more human recognizable component too, but you need to be very very careful.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/

peixe01
Posts: 7
Joined: 2011-07-11T13:41:39-07:00
Authentication code: 8675308

Re: Merging 2 gifs, one is animated and transparent

Post by peixe01 » 2011-07-14T03:27:25-07:00

fmw42 wrote: Can you post your php file so that one can see the code and not just the image display?

Are you using the same version of IM with PHP as with the command line?
Here's the code:

Code: Select all

$possible = "112346789ABCDFGHJKLMNPQRTVWXYZ";

while (strlen(trim($captcha)) < 7) 
	{
	$char = substr($possible, mt_rand(1, strlen($possible)), 1);
	$captcha .= $char." ";
	}
	
$text = trim($captcha);

$image = "background.gif"; 
$font = "ARIALBD.TTF";
$font_size = "18";

$image_2 	= imagecreatefromgif($image);
$black 		= imagecolorallocate($image_2,0,0,0);
$white 		= imagecolorallocate($image_2, 255, 255, 255);
$grey 		= imagecolorallocate($image_2, 30, 35, 41);

$image_width = imagesx($image_2);  
$image_height = imagesy($image_2);

$text_box = imagettfbbox($font_size,$angle,$font,$text);
$text_width = $text_box[2]-$text_box[0]; // lower right corner - lower left corner
$text_height = $text_box[3]-$text_box[1];

$x = ($image_width/2) - ($text_width/2);
$y = ($image_height/2) - ($text_height/2)+8;

imagettftext($image_2,$font_size,0,$x,$y,$grey,$font,$text );

imagegif($image_2,"/home/**/public_html/captcha/ver2/text.gif");


$path = "/home/**/public_html/captcha/ver2";

$cmd = 'convert '.$path.'/wheel.gif -coalesce \-draw \'image DstOver 0,0 0,0 "'.$path.'/text.gif" \' \-layers Optimize '.$path.'/captcha.gif';

exec("$cmd 2>&1", $err); 
I didnt even know there could be 2 versions installed, phpinfo() doesnt say anything about IM... how would i find this out?
Last edited by peixe01 on 2011-07-14T03:32:34-07:00, edited 1 time in total.

peixe01
Posts: 7
Joined: 2011-07-11T13:41:39-07:00
Authentication code: 8675308

Re: Merging 2 gifs, one is animated and transparent

Post by peixe01 » 2011-07-14T03:32:15-07:00

anthony wrote:As a catchpa it is easily defeated. Just look for the pixels that don't change much and you can remove the animated overlay!
A little morphology and the text becomes clear, ready for OCP to convert to plain text.

A animated distortion of the text as well may help so as to remove any 'static' parts to the image.

However in general animations provide far more information for the catchpa solvers to use in breaking it.
Of course it can also provide more human recognizable component too, but you need to be very very careful.
thanks for the input anthony, i though captcha solvers had a hard time with animated captchas humm, this is for a browser game, i dont want to piss off the players much so i am running away from those really hard captchas that even humans cant read lol

Since i posted my code above, how would you morph the text a little, do you think that alone would greatly improve its effectiveness?

User avatar
fmw42
Posts: 25654
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Merging 2 gifs, one is animated and transparent

Post by fmw42 » 2011-07-14T09:51:44-07:00

I didnt even know there could be 2 versions installed, phpinfo() doesnt say anything about IM... how would i find this out?
In your exec command, put one of these and see what you get back for each

convert -version

which convert

type convert

or

<?php
echo "<pre>";
system("which convert");
echo "</pre>";
?>


<?php
exec("convert -version",$out,$returnval);
print_r($out[0]);
?>

or put your full path in like the following if the above does not work

<?php
exec("/usr/local/bin/convert -version",$out,$returnval);
print_r($out[0]);
?>

One of the PHP experts will have to examine and critique your code. I am not that much of a PHP expert. Perhaps it is your version of IM and how it handles all the grayshades from each frame of the gif when optimizing?

User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Merging 2 gifs, one is animated and transparent

Post by anthony » 2011-07-14T18:42:20-07:00

peixe01 wrote:
anthony wrote:As a catchpa it is easily defeated. Just look for the pixels that don't change much and you can remove the animated overlay!
A little morphology and the text becomes clear, ready for OCP to convert to plain text.

A animated distortion of the text as well may help so as to remove any 'static' parts to the image.

However in general animations provide far more information for the catchpa solvers to use in breaking it.
Of course it can also provide more human recognizable component too, but you need to be very very careful.
thanks for the input anthony, i though captcha solvers had a hard time with animated captchas humm, this is for a browser game, i dont want to piss off the players much so i am running away from those really hard captchas that even humans cant read lol

Since i posted my code above, how would you morph the text a little, do you think that alone would greatly improve its effectiveness?
At the moment they may not understand a animated catchpa. But as soon as people start making use of them, they will evolve. It is a jungle out their on in the Matrix, and programs continually evolve into better predators to match the evolving prey. I just pointing out that if I can think of a simple way to defeat the specific example you gave. The predators will too.

However don't give up. Humans can see patterns in moving images, where when presented with each individual static frame of the animation even they couldn't see anything, and for a catchpa that can be a good thing. You want to find the human in the crowd. A catchpa is essentially a type of "Turing Test" after all.

For example think of looking though a slatted fence. When you are not moving all you really see is the fence, but if you're moving you can see everything that is behind that fence quite clearly. That is what you need to aim for, just don't make the 'static image' behind that 'fence' too 'static' or the 'fence' too plain.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/

Post Reply