#!/usr/bin/env python
# -*- coding: utf-8 -*-
import random
import Image
import ImageFont
import ImageDraw
import ImageFilter
import gtk
import pygtk
import os
import threading
gtk.gdk.threads_init()
#--------------------------imagen construir--------------------------------
def gen_random_word(wordLen=6):
allowedChars = "abcdefghijklmnopqrstuvwzyzABCDEFGHIJKLMNOPQRSTUVWZYZ0123456789"
word = ""
for i in range(0, wordLen):
word = word + allowedChars[random.randint(0,0xffffff) % len(allowedChars)]
return word
def gen_captcha(text, fnt, fnt_sz, file_name, fmt='JPEG'):
fgcolor = random.randint(0,1)
bgcolor = fgcolor ^ 0xffffff
font = ImageFont.truetype(fnt,fnt_sz)
dim = font.getsize(text)
im = Image.new('RGB', (dim[0]+5,dim[1]+5), bgcolor)
d = ImageDraw.Draw(im)
x, y = im.size
r = random.randint
for num in range(100):
d.rectangle((r(0,x),r(0,y),r(0,x),r(0,y)),fill=r(0,0xffffff))
d.text((3,3), text, font=font, fill=fgcolor)
im = im.filter(ImageFilter.EDGE_ENHANCE_MORE)
im.save(file_name, format=fmt)
#--------------------------------------------------------------------------
class PackBox1:
def __init__(self):
self.word = gen_random_word()
gen_captcha(self.word.strip(), '/usr/share/fonts/truetype/freefont/FreeSansBoldOblique.ttf', 25, "test.jpg")
window = gtk.Window(gtk.WINDOW_TOPLEVEL)
window.set_title("-* C A N A I M A *-")
window.set_border_width(10)
window.set_size_request(550, 200)
window.set_resizable(False)
def imagen_box2(homogeneous, spacing, expand, fill, padding):
caja = gtk.HBox(homogeneous, spacing)
caja.set_border_width(5)
self.image = gtk.Image()
self.image.set_from_file('test.jpg')
#self.image.connect("clicked", self.refresh_captcha)
caja.pack_start(self.image, True, False,0)
self.image.show()
return caja
def make_box1(homogeneous, spacing, expand, fill, padding):
def clic_boton2(widget):
hilo = threading.Thread(target=captcha_fun, args=(widget))
hilo.start()
def captcha_fun(widget):
if texto.get_text() != self.word:
systema= os.system("gedit /tmp/Documento.txt")
gtk.gdk.threads_enter()
md=gtk.MessageDialog(parent=None, flags=0, type=gtk.MESSAGE_ERROR, buttons=gtk.BUTTONS_CLOSE, message_format="El valor introducido no coincide con el captcha intente de nuevo")
md.run()
md.destroy()
self.refresh_captcha()
gtk.gdk.threads_leave()
else:
gtk.gdk.threads_enter()
md=gtk.MessageDialog(parent=None, flags=0, buttons=gtk.BUTTONS_OK, message_format="El valor introducido es Correcto")
md.run()
md.destroy()
self.refresh_captcha()
gtk.gdk.threads_leave()
caja = gtk.HBox(homogeneous, spacing)
caja.set_border_width(10)
etiqueta = gtk.Label("Introduzca el valor")
#etiqueta.set_alignment(0,0)
caja.pack_start(etiqueta, False, False, 30)
etiqueta.show()
texto = gtk.Entry(10)
#texto.connect("activate", enter_callback)
caja.pack_start(texto, False, False, 10)
texto.show()
boton = gtk.Button(stock=gtk.STOCK_OK)
boton.connect("clicked", clic_boton2)
caja.pack_start(boton, True, True, 40)
boton.show()
return caja
def make_box3(homogeneous, spacing, expand, fill, padding):
caja = gtk.HBox(homogeneous, spacing)
caja.set_border_width(10)
boton = gtk.Button(stock=gtk.STOCK_CLOSE)
boton.connect("clicked", gtk.mainquit)
caja.pack_start(boton, gtk.TRUE, gtk.TRUE, 70)
boton.show()
return caja
box1 = gtk.VBox(False, 0)
box2 = imagen_box2(False, 0, False, False,0)
box1.pack_start(box2, False, False, 0)
box2.show()
box2 = make_box1(False, 0, False, False,0)
box1.pack_start(box2, False, False, 0)
box2.show()
box2 = make_box3(False, 0, False, False,0)
box1.pack_start(box2, False, False, 20)
box2.show()
box1.show()
window.add(box1)
window.show()
def refresh_captcha(self):
self.word = gen_random_word()
gen_captcha(self.word.strip(), '/usr/share/fonts/truetype/freefont/FreeSansBoldOblique.ttf', 25, "test.jpg")
self.image.set_from_file('test.jpg')
def main():
gtk.main()
return 0
if __name__ == '__main__':
packbox1 = PackBox1()
main()