Generating Image Captcha in Python Using graphic-verification-code and captcha Libraries

This tutorial demonstrates two Python approaches for creating image verification codes—using the graphic-verification-code library for a quick four‑line solution and the captcha library for customizable random captchas—complete with installation commands, code examples, and sample outputs.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
Generating Image Captcha in Python Using graphic-verification-code and captcha Libraries

The article presents two practical methods for generating image captcha (verification code) images in Python.

Method 1: graphic-verification-code library Install the library with pip install graphic-verification-code. Then import and generate a captcha image and its corresponding text:

import gvcode
a, m = gvcode.generate()  # unpack the tuple
a.show()                     # display the generated image
print(m)                     # print the captcha text

The resulting image and the printed code are shown as the execution result.

Method 2: captcha library Install the library with pip install captcha. Use ImageCaptcha to create an image from a given string:

from captcha.image import ImageCaptcha
img = ImageCaptcha().generate_image("176AK0")
img.show()

This also displays the generated image.

Advanced usage: random 4‑character captcha By importing randint and defining a list of alphanumeric characters, you can generate a random 4‑character string and create its image:

from captcha.image import ImageCaptcha
from random import randint
list = ['0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z']

data = ''
for i in range(4):
    data += list[randint(0,62)]
img = ImageCaptcha().generate_image(data)
img.show()

Sample output images for each method are included in the original article.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

SecurityCaptchacaptcha-librarygraphic-verification-codeimage verificationcode-example
Python Programming Learning Circle
Written by

Python Programming Learning Circle

A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.