Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to create an Captcha with TclMagick?

82 views
Skip to first unread message

Einstein30000

unread,
Sep 28, 2007, 3:06:37 PM9/28/07
to
Hi,

to protect my self-made board against Spambots a captcha is necessary.
So i have to create one using TclMagick.

My current try on apache 1. Webserver (with installed rivet):

<?
package require TclMagick
set wand [magick create wand]
set draw [magick create drawing]
set rpx [magick create pixel red]
$rpx color red
$draw SetStrokeColor red
$draw SetFillColor red
$draw color 1 1 point
$wand SetSize 100 30
$wand DrawImage $draw
?>

This small script doesn't work and i can't understand the syntax of
TclMagick?! :-\

I hope someone could help me with this Problem, all i want is:

A small Captcha graphic (any resolution but not to big!) with 5
radomly selected chars (a-z, A-Z, 0-9). The chars should also have an
randomly selected rotation between 0 and 30 degrees.

The script above was my try to create only a small image to test
TclMagick, but it ends with the following error:

Wand contains no images `MagickWand-3'
while executing
"$wand DrawImage $draw"
(in namespace eval "::request" script line 13)
invoked from within
"namespace eval request {
puts -nonewline ""

I hope someone could help me with this Problem!

cheers
Dominic

sp...@controlq.com

unread,
Sep 28, 2007, 6:25:26 PM9/28/07
to
On Fri, 28 Sep 2007, Einstein30000 wrote:

> Date: Fri, 28 Sep 2007 12:06:37 -0700
> From: Einstein30000 <domini...@web.de>
> Newsgroups: comp.lang.tcl
> Subject: How to create an Captcha with TclMagick?


>
> Hi,
>
> to protect my self-made board against Spambots a captcha is necessary.
> So i have to create one using TclMagick.
>
> My current try on apache 1. Webserver (with installed rivet):
>
> <?
> package require TclMagick
> set wand [magick create wand]
> set draw [magick create drawing]
> set rpx [magick create pixel red]
> $rpx color red
> $draw SetStrokeColor red
> $draw SetFillColor red
> $draw color 1 1 point
> $wand SetSize 100 30
> $wand DrawImage $draw
> ?>
>
> This small script doesn't work and i can't understand the syntax of
> TclMagick?! :-\
>

Einstein,

Actually, I posted some months back something you might find useful, and
I'll repost it if it helps ... the first part is simple image
manipulation, and the second addresses annotating images, which is, I
believe what you are referring to (caption?) ...

The following snippet:

creates a photo image, creates a wand, reads a file into the wand,
and then copies the wand into the photo, stuffs the pic onto a label,
and then displays it ... Tclmagick allows you to create the images
dynamically, (rather than read them from files), and you can annotate
them, resize them etc. and then write them to a photo image, and display
Hope this helps ...

Cheers,
Rob Sciuk

-=-=-=-=-=

package require img::jpeg
package require TclMagick

set fil /mnt/home/rob/DogPen.jpg
set pic [image create photo]
set wnd [magick create wand]
$wnd read $fil
$pic put [$wnd writeblob]

set lbl [label .l -image $pic]
pack $lbl

-=-=-=-=-=

Also, when I went to figure out how to annotate images, it was a little
less than transparent (and I'm not talking about alpha channels here
necessarily), so you might find this code useful, I hope ...

proc drawWand { } {
return [magick create draw]
}
proc drawColour { colour } {
set pxl [magick create pixel]
$pxl SetColor $colour
return $pxl
}
proc annotate { wnd x y angle txt } {
set clr [qim::drawColour cyan ]
set drw [qim::drawWand]
$drw SetStrokeWidth 1.0
$drw SetStrokeColor $clr
$drw SetFillColor $clr
$drw SetStrokeOpacity .8
$drw SetFillOpacity .8
$drw SetFontSize 24
$drw rotate $angle
$drw Annotation $x $y $txt
$wnd DrawImage $drw
magick delete $drw
magick delete $clr
}

used:

annotate $wnd 20 20 0 "This is my Caption"

Hope this helps ...

Cheers,
Rob.

Pete

unread,
Sep 28, 2007, 9:57:52 PM9/28/07
to
Einstein30000 wrote:
> to protect my self-made board against Spambots a captcha is necessary.
> So i have to create one using TclMagick.
> Dominic

I have a TCL script which demonstrates generation of a captcha. It uses
a fixed string but that, and some other features of the script, can
easily be randomized.

Hope this helps.
Pete (to reply: NN = 01)


# Demo to generate a captcha
package require TclMagick
set m [magick create wand]
set n [magick create wand]
set d [magick create draw]
set p [magick create pixel]

# Create a large white canvas to give lots of room for the text
# Later on it will be trimmed down
$p color white
$m new 640 640 $p

# Now select the font and set its colour and size
$p color red
$d fillcolor $p
$d font Times-New-Roman
$d fontsize 24

# and annotate the image *without* any rotation
$m annotate $d 100 100 0 "Z R B Y A"

# Trim the image to remove all the excess surrounding the text
$m trim
# Now mangle it a bit. The values for swirl and wave should be randomized
$m swirl 20
$m wave 4 50
# Get the size of the current image
set w [$m width]
set h [$m height]
# increase the width and height to allow for a border around the text
incr w 10
incr h 6
# Create a new white image
$p color white
$n new $w $h $p
# Add noise to it
$n addnoise Gaussian
# Composite the text image on the noise image
# The coordinate for the composite should be half the border dimensions
# so that the text image is centred over the noise image
$n composite $m Multiply 5 3
# And save the image
$n write test.jpg
# Clean up
magick delete $m $d $p $n


Einstein30000

unread,
Sep 29, 2007, 6:04:06 AM9/29/07
to
Hi,

to test it, i've Executed both Scripts, at the moment i use TclMagick
0.45. But both Scripts return an error when the command 'DrawImage'
was executed.

An example:

I start tclsh an execute:

% package require TclMagick
0.45
% set wnd [magick create wand]
magick8118C58
% set x 20
20
% set y 20
20
% set angle 0
0
% set txt "Tcl/Tk"
Tcl/Tk
% set clr [magick create pixel]
magick811DD88
% $clr SetColor cyan
% set drw [magick create draw]
magick8124F60
% $drw SetStrokeWidth 1.0
% $drw SetStrokeColor $clr
% $drw SetFillColor $clr
% $drw SetStrokeOpacity .8
% $drw SetFillOpacity .8
% $drw SetFontSize 24
% $drw rotate $angle
% $drw Annotation $x $y $txt
% $wnd DrawImage $drw

(I've typed it per hand)

But the last command returns: "Wand contains no images `MagickWand-1'"

The variable 'errorInfo' contains:

Wand contains no images `MagickWand-1'
while executing
"$wnd DrawImage $drw"

Could someone help me please?

Pete

unread,
Sep 29, 2007, 11:33:59 AM9/29/07
to
Pete wrote:

> I have a TCL script which demonstrates generation of a captcha.

Sorry, there's an error in the script. When I tested it, I already had
my TclWand package loaded which adds the "new" subcommand. But "new"
isn't valid in TclMagick.

Change these two lines:


> $p color white
> $m new 640 640 $p

To this:
$m size 640 480
$m read xc:white

Pete
(to reply: NN=01)

Pete

unread,
Sep 29, 2007, 11:56:59 AM9/29/07
to
Einstein30000 wrote:

> Could someone help me please?
>

In order to draw on a wand it must already have an image.
After you create wnd, do this:
$wnd size 400 200
$wnd read xc:none

This will create an initial 400x200 image as a transparent background.
You can use, for example, xc:white to have an opaque white background or
any other colour you choose.

I posted a slight fix to my captcha script. Apply that fix and try the
script.

Pete
(To reply: NN=01)

Einstein30000

unread,
Sep 30, 2007, 7:01:24 AM9/30/07
to
Hi,

thank you very much! The script is working and now it's in use on my
website.

cheers
Dominic

0 new messages