Showing posts with label python. Show all posts
Showing posts with label python. Show all posts

Thursday, November 25, 2010

Captcha Prompter

  • This tool is written using PyGTK2. It takes an image file name as input, shows the image to the user and asks him to type the text shown in the image. The goal is to use it inside the Unix scripts. For example, one could do 'CAPTCHA_TEXT=$(python prompt_captcha.py captcha.jpg)' in Bash and perform further actions with $CAPTCHA_TEXT. This little tool will accept png, jpg, gif and most other commonimage formats.

#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
prompts user to solve captcha image.

This tool is written using PyGTK2. It takes an image file name as input, shows the image to the user and asks him to type the text shown in the image. The goal is to use it inside the Unix scripts. For example, one could do 'CAPTCHA_TEXT=$(python prompt_captcha.py captcha.jpg)' in Bash and perform further actions with $CAPTCHA_TEXT. This little tool will accept png, jpg, gif and most other common image formats.

[*] usage: python prompt_captcha.py captcha_image
"""

__author__ =  'Babil (Golam Sarwar)'
__version__=  '0.1'

import sys
import time
import Image
import gtk
import pygtk


pygtk.require('2.0')

class getCaptcha(object):
    """performs all the GTK2 gui actions"""

    def enter_callback(self, widget, textbox):
        """callback funcion for when user presses the enter key"""
 textbox_text = textbox.get_text()
        print textbox_text
 gtk.main_quit()

    def __init__(self, captcha=None):
        #
 if captcha == None:
  print >> sys.stderr, "[*] usage: python prompt_captcha.py captcha_image"
  sys.exit(-1)

 width, height = Image.open(captcha).size
 
 window = gtk.Window(gtk.WINDOW_TOPLEVEL)
 window.set_geometry_hints(window,width+15,height+15,width+30,height+30)
        window.set_size_request(width, height)
 window.resize(width+15, height+15)
        window.set_title("GTK textbox")
        window.connect("delete_event", lambda w,e: gtk.main_quit())
 window.set_title("Enter Captcha")
 window.set_keep_above(True)
 window.set_urgency_hint(True)
 window.move(gtk.gdk.screen_width()/2 - (width+15)/2, gtk.gdk.screen_height()/2 - (height+15)/2)


        vbox = gtk.VBox(False, 0)
        window.add(vbox)

 image = gtk.Image()
 image.set_from_file(captcha)

 button = gtk.Button()
 button.set_image(image)
 button.set_relief(gtk.RELIEF_HALF)
 #button.set_tooltip_text("Type captcha then press enter \n or click the image.")
        vbox.pack_start(button, True, True, 0)

 textbox = gtk.Entry()
        textbox.set_max_length(50)
        textbox.connect("activate", self.enter_callback, textbox)
 textbox.set_text("Enter captcha & then click on the image :-) ")
        textbox.select_region(0, len(textbox.get_text()))
 vbox.pack_start(textbox, True, True, 10)


 button.connect("clicked", self.enter_callback, textbox)
 window.show()
        vbox.show()
 button.show()
 image.show()
 textbox.show()
 textbox.grab_focus()



def main():
    gtk.main()
    return 0

if __name__ == "__main__":
 if len(sys.argv) >=2: 
  getCaptcha(sys.argv[1])
 else: 
  getCaptcha()
 main()

Tuesday, October 19, 2010

IDLE not working in ActivePython

The problem is that "idle.bat" is missing some double-quotes. If you have installed Python in "C:\Programs files\Python27" instead of "C:\Python27\", "idle.bat" will fail to execute "idle.pyw".

Copy and paste the follow 2 lines in "C:\Program Files\Python27\Lib\idlelib\idle.bat" (or wherever "idle.bat" is located) to fix it:


@echo off
rem Start IDLE using the appropriate Python interpreter
set CURRDIR=%~dp0
start "%CURRDIR%..\..\pythonw.exe" "%CURRDIR%idle.pyw" %1 %2 %3 %4 %5 %6 %7 %8 %9

Friday, September 24, 2010

Forcing Floating Point Calculation in Python

Sources:
  • http://docs.python.org/tutorial/floatingpoint.html
  • http://stackoverflow.com/questions/1267869/how-can-i-force-division-to-be-floating-point-in-python

Method-1:Just write any of the parameters in the division operation in floating-point format (eg. 4.0, 3.0)
>>> 4.0 /  3
or, >>> 4 / 3.0

Method-2:
from __future__ import devision
Method-3:
>>> from decimal import Decimal
>>> Decimal(2.675)
Decimal('2.67499999999999982236431605997495353221893310546875')