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:
import random, string, tkinter as tk: Importing therandom,string, andtkintermodules.tkinteris renamed astkfor easier use.from tkinter import *: Importing all functions and objects from thetkintermodule.from PIL import Image, ImageTk: Importing theImageandImageTkmodules from the Pillow (PIL) library for image processing.from captcha.image import ImageCaptcha: Importing theImageCaptchaclass from thecaptcha.imagemodule.def CreateWidgets():: Defining a function calledCreateWidgets()to create the necessary tkinter widgets.root.captchaLabel = Label(...: Creating a Label widget (captchaLabel) to display the generated captcha image.generateBTN = Button(...: Creating a Button widget (generateBTN) to generate the captcha image.def generateCaptcha():: Defining a function calledgenerateCaptcha()to generate the captcha image.root.captchaLabel.config(width=300, height=100): Configuring the width and height of thecaptchaLabel.captcha_image = ImageCaptcha(width=280, height=90): Creating anImageCaptchaobject with specified width and height.captcha_text = ''.join(...): Generating a random string of characters (letters and digits) to use as the captcha text.captcha_data = captcha_image.generate(captcha_text): Generating the image of the captcha text using thegenerate()method of thecaptcha_imageobject.imageView = Image.open(captcha_data): Opening the image of the captcha data using theopen()method of theImageclass.imageDisplay = ImageTk.PhotoImage(imageView): Creating aPhotoImageobject to display the captcha image.root.captchaLabel.config(image=imageDisplay): Configuring thecaptchaLabelto display the captcha image.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.





Leave a Reply