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 »

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: 25562
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 »

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 »

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: 25562
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 »

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 »

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: 25562
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 »

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 »

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 »

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 »

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: 25562
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 »

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 »

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 »

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 »

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: 25562
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 »

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 »

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