Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to generate CAPTCHA by Python

2025-03-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

How to generate CAPTCHA for Python, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.

Python generates CAPTCHA. Articles have no practical information.

Crawlers and CAPTCHA are natural enemies, so generating CAPTCHA with Python should be the first step against CAPTCHA with Python, so today we will first study how to use Python to generate a CAPTCHA that is convenient for us to start with.

Here is not to build wheels, just step on the pit, at present, there are many open source projects on Github about CAPTCHA confrontation, here recommend two, experienced friends directly skip the following content, just learn these two projects.

CAPTCHA training: (the following introduction comes from the official introduction)

Https://github.com/kerlomz/captcha_trainer

Image CAPTCHA solution based on deep learning-this project can kill all kinds of interference such as character adhesion overlap / perspective distortion / blur / noise, which is enough to solve most of the complex CAPTCHA scenarios on the market, and is also used in other OCR scenarios.

The project, based on TensorFlow 1.14, aims to help small and medium-sized enterprises or individual users quickly build image classification models and put them into production environment, so as to reduce the threshold of technology application.

CAPTCHA deployment:

Https://github.com/kerlomz/captcha_platform

About deployment, the boss also provided the corresponding article: https://www.jianshu.com/p/80ef04b16efc

Generate a simple CAPTCHA

The library captcha needs to be installed here.

Pip install captcha

Next, you can get a simple CAPTCHA image using the following code

From captcha.image import ImageCaptcha

Import matplotlib.pyplot as plt

Import random

Import string

# defines the character set as numbers and characters

Chs=string.digits+string.ascii_uppercase

Print (chs)

# define length, width, number of characters, category

Width,height,n_len,n_class=170,80,4,len (chs)

# define the width and height of the picture

Generator=ImageCaptcha (width=width,height=height)

# generate a 4-bit random string

Random_str= "" .join ([random.choice (chs) for i in range (4)])

Print (random_str)

# generate random verification codes

Img=generator.generate_image (random_str)

Plt.imshow (img)

Plt.title (random_str)

Plt.show ()

You can get a picture like this at this time.

Generate multiple CAPTCHA codes in batch

Just such a picture will not meet my needs, so change the above code and turn it into a program to generate multiple pictures.

From captcha.image import ImageCaptcha

Import matplotlib.pyplot as plt

Import numpy as np

Import random

Import string

Chs = string.digits + string.ascii_uppercase

Width, height, n_len, n_class = 170,80,4, len (chs)

Def createxy (batch_size=100):

X = np.zeros ((batch_size, height, width, 3), dtype=np.uint8)

Y = [np.zeros ((batch_size, n_class), dtype=np.uint8) for i in range (n_len)]

Generator = ImageCaptcha (width=width, height=height)

While True:

For i in range (batch_size):

# Random string

Random_str = "" .join ([random.choice (chs) for j in range (4)])

# generate CAPTCHA based on random string

X [I] = generator.generate_image (random_str)

For j, ch in enumerate (random_str):

Y [j] [I,:] = 0

Y [j] [I, chs.find (ch)] = 1

Yield X, Y

Def getycode (y):

# reduce the dimension and take out non-zero data

Y = np.argmax (np.array (y), axis=2) [:, 0]

# displays a fetched character equal to 1

Return "" .join ([CHS [x] for x in y])

X, Y = next (createxy (1))

Plt.imshow (X [0])

Plt.title (getycode (Y))

Plt.show ()

After finishing the above, I can start to try to train, but I haven't learned it, so I'll talk about it next time.

This is the answer to the question about how Python generates CAPTCHA. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel to learn more about it.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report