Skip to content

Instantly share code, notes, and snippets.

@JayantGoel001
Last active November 9, 2023 01:25
Show Gist options
  • Save JayantGoel001/575afbffa6b35fbbe40aa2cf15731cda to your computer and use it in GitHub Desktop.
Save JayantGoel001/575afbffa6b35fbbe40aa2cf15731cda to your computer and use it in GitHub Desktop.
Creating Image and Audio Captcha using python
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import random"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"def generateText(length=8,t=\"text\"):\n",
" i = 0\n",
" text = \"\"\n",
" while i<length:\n",
" if t!=\"number\":\n",
" p = random.randint(1,3)\n",
" else:\n",
" p = 3\n",
" if p==1:\n",
" text += chr(random.randint(ord('A'),ord('Z')))\n",
" elif p==2:\n",
" text += chr(random.randint(ord('a'),ord('z')))\n",
" else:\n",
" text += chr(random.randint(ord('0'),ord('9')))\n",
" i+=1\n",
" return text"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Image Captcha"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"from captcha.image import ImageCaptcha"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"image = ImageCaptcha(width=250,height=100)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"text = generateText()\n",
"image.write(text,'captcha.png')"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'om25hlpC'"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"text"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Audio Captcha"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"from captcha.audio import AudioCaptcha"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"audio = AudioCaptcha()"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"155480"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"text = generateText(t=\"number\")\n",
"audio.write(text,'captcha.wav')"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'97439472'"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"text"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.5"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
@JayantGoel001
Copy link
Author

captcha

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment