,

Python Tkinter Captcha Generator – Amazing Project

The Python GUI Captcha Generator is an amazing project that allows you to create and display CAPTCHA images using a graphical user interface (GUI). CAPTCHA images are used to distinguish between human users and automated bots by presenting challenges that are easy for humans to solve but difficult for bots. This project uses the captcha library in Python to generate CAPTCHA images and tkinter library for the GUI. With this project, you can generate CAPTCHA images with random text and display them on the screen.

Let’s check out the full code given below and the output

# PYTHON GUI TO GENERATE CAPTCHA IMAGE USING THE captcha LIBRARY OF PYTHON

# The captcha Library is capable of generating Audio and Image CAPTCHAs.

# The module can be installed using the command - pip install captcha

# Importing the necessary packages
import random, string, tkinter as tk
from tkinter import *
from PIL import Image, ImageTk
from captcha.image import ImageCaptcha

# Defining CreateWidgets() function to create necessary tkinter widgets
def CreateWidgets():
    root.captchaLabel = Label(root, bg="lightskyblue4", borderwidth=3, relief="groove", width=33, height=6)
    root.captchaLabel.grid(row=1, column=0, padx=10, pady=10, columnspan=2)

    generateBTN = Button(root, width=10, text="GENERATE", command=generateCaptcha)
    generateBTN.grid(row=2, column=0, columnspan=2, padx=5, pady=5)

# Defining a function named generateCaptcha() to generate captcha image of random string
def generateCaptcha():
    # Setting the width and height of the captchaImage Label
    root.captchaLabel.config(width=300, height=100)
    # Creating an object of ImageCaptcha() with height and width parameters
    captcha_image = ImageCaptcha(width=280, height=90)
    # Generating random string of random length using random library to be used as captcha text
    captcha_text = ''.join(random.choices(string.ascii_letters + string.digits, k=random.randint(5, 7)))
    # Generating image of the captcha_text using the generate() method of captcha_image object
    captcha_data = captcha_image.generate(captcha_text)
    # Captch Image can be saved using the write() method of the captcha_image object of ImageCaptcha()
    # captcha_image.write(captcha_text, "IMAGE_NAME.png")
    # Opening the image of captcha_data using open() method of Image class which takes image as argument
    imageView = Image.open(captcha_data)
    # Creating object of PhotoImage() class to display the frame
    imageDisplay = ImageTk.PhotoImage(imageView)
    # Configuring the label to display the frame
    root.captchaLabel.config(image=imageDisplay)
    # Keeping a reference
    root.captchaLabel.photo = imageDisplay

# Creating object of tk class
root = tk.Tk()

# Setting the title and background color
root.geometry("325x170")
root.title("PythonCaptchaGenerator")
root.config(background="deepskyblue4")

# Calling the CreateWidgets() function
CreateWidgets()

# Defining infinite loop to run application
root.mainloop()

Output:

Explanation of the above code:

  1. import random, string, tkinter as tk: Importing the random, string, and tkinter modules. tkinter is renamed as tk for easier use.
  2. from tkinter import *: Importing all functions and objects from the tkinter module.
  3. from PIL import Image, ImageTk: Importing the Image and ImageTk modules from the Pillow (PIL) library for image processing.
  4. from captcha.image import ImageCaptcha: Importing the ImageCaptcha class from the captcha.image module.
  5. def CreateWidgets():: Defining a function called CreateWidgets() to create the necessary tkinter widgets.
  6. root.captchaLabel = Label(...: Creating a Label widget (captchaLabel) to display the generated captcha image.
  7. generateBTN = Button(...: Creating a Button widget (generateBTN) to generate the captcha image.
  8. def generateCaptcha():: Defining a function called generateCaptcha() to generate the captcha image.
  9. root.captchaLabel.config(width=300, height=100): Configuring the width and height of the captchaLabel.
  10. captcha_image = ImageCaptcha(width=280, height=90): Creating an ImageCaptcha object with specified width and height.
  11. captcha_text = ''.join(...): Generating a random string of characters (letters and digits) to use as the captcha text.
  12. captcha_data = captcha_image.generate(captcha_text): Generating the image of the captcha text using the generate() method of the captcha_image object.
  13. imageView = Image.open(captcha_data): Opening the image of the captcha data using the open() method of the Image class.
  14. imageDisplay = ImageTk.PhotoImage(imageView): Creating a PhotoImage object to display the captcha image.
  15. root.captchaLabel.config(image=imageDisplay): Configuring the captchaLabel to display the captcha image.
  16. root.mainloop(): Running the main event loop of the tkinter application to display the GUI and handle events.

Conclusion: In conclusion, the Python GUI Captcha Generator is a fun and educational project that demonstrates how to create a graphical user interface (GUI) for generating CAPTCHA images. By using the captcha library in Python, you can easily generate random text for the CAPTCHA images and display them using tkinter. This project is a great way to learn about GUI programming in Python and how to use libraries for generating and displaying images.

Author

Sona Avatar

Written by

Leave a Reply

Trending

CodeMagnet

Your Magnetic Resource, For Coding Brilliance

Programming Languages

Web Development

Data Science and Visualization

Career Section

<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-4205364944170772"
     crossorigin="anonymous"></script>