


What we are going to learn today is how to use Python to generate a random Chinese verification code and save the image in .jpeg format. Friends who need it can refer to it
Before this, you must first understand PIL library in Python. PIL is the abbreviation of Python Imaging Library. PIL is a Python library for image processing. It provides a series of modules and methods, such as: cropping, translation, rotation, changing size, etc. In the PIL library, any image is represented by an Image object, so to load an image, the simplest form is as follows:
from PIL import Image image = Image.open("1.jpeg")
In the PIL library, the most commonly used modules are Image, ImageDraw, ImageEnhance, ImageFile, etc.
The download address of PIL is: http://www.pythonware.com/products/pil/
Next, learn some common operations in PIL:
(1) Change the picture size
from PIL import Image image = Image.open("1.jpeg") new_img = image.resize((256,256),Image.BILINEAR) new_img.save("2.jpeg")
(2) Rotate the picture
from PIL import Image image = Image.open("1.jpeg") new_img = image.rotate(45) new_img.save("2.jpeg")
(3) Straight line drawing
The ImageDraw module in the PIL library provides basic functions of graphics drawing, which can draw straight lines, arcs, ellipses, rectangles, etc.
from PIL import Image,ImageDraw image = Image.open("2.jpeg") draw = ImageDraw.Draw(image) width,height = image.size draw.line(((0,0),(width-1,height-1)),fill=255) draw.line(((0,height-1),(width-1,0)),fill=255) image.save("2.jpeg")
(4) Draw a circle
from PIL import Image,ImageDraw image = Image.open("2.jpeg") draw = ImageDraw.Draw(image) width,height = image.size draw.arc((0,0,width-1,height-1),0,360,fill=255) image.save("2.jpeg")
More methods are available See help documentation.
Now let’s study how to use Python to generate Chinese verification codes. In fact, this question is simple. We all know that the verification code is generally used to prevent network machine
people from using unlimited login attempts to crack the password. Then we need to randomly generate Chinese characters for this picture to verify. Generally speaking The word
in the picture will appear very blurry, which effectively prevents robot recognition. So in addition to randomly generating Chinese characters, we also need to add some interference lines and so on.
# -*- coding: utf-8 -*- from PIL import Image,ImageDraw,ImageFont import random import math, string class RandomChar(): @staticmethod def Unicode(): val = random.randint(0x4E00, 0x9FBF) return unichr(val) @staticmethod def GB2312(): head = random.randint(0xB0, 0xCF) body = random.randint(0xA, 0xF) tail = random.randint(0, 0xF) val = ( head << 8 ) | (body << 4) | tail str = "%x" % val return str.decode('hex').decode('gb2312') class ImageChar: def __init__(self, fontColor = (0, 0, 0), size = (100, 40), fontPath = 'SIMSUN.TTC', bgColor = (255, 255, 255), fontSize = 20): self.size = size self.fontPath = fontPath self.bgColor = bgColor self.fontSize = fontSize self.fontColor = fontColor self.font = ImageFont.truetype(self.fontPath, self.fontSize) self.image = Image.new('RGB', size, bgColor) def rotate(self): self.image.rotate(random.randint(0, 30), expand=0) def drawText(self, pos, txt, fill): draw = ImageDraw.Draw(self.image) draw.text(pos, txt, font=self.font, fill=fill) def randRGB(self): return (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)) def randPoint(self): (width, height) = self.size return (random.randint(0, width), random.randint(0, height)) def randLine(self, num): draw = ImageDraw.Draw(self.image) for i in range(0, num): draw.line([self.randPoint(), self.randPoint()], self.randRGB()) def randChinese(self, num): gap = 5 start = 0 for i in range(0, num): char = RandomChar().GB2312() x = start + self.fontSize * i + random.randint(0, gap) + gap * i self.drawText((x, random.randint(-5, 5)), RandomChar().GB2312(), self.randRGB()) self.rotate() self.randLine(18) def save(self, path): self.image.save(path) ic = ImageChar(fontColor=(100,211, 90)) ic.randChinese(4) ic.save("1.jpeg")
Running results:
More pythonPIL modules and randomly generated Chinese verification codes For method-related articles, please pay attention to the PHP Chinese website!

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于Seaborn的相关问题,包括了数据可视化处理的散点图、折线图、条形图等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于进程池与进程锁的相关问题,包括进程池的创建模块,进程池函数等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于简历筛选的相关问题,包括了定义 ReadDoc 类用以读取 word 文件以及定义 search_word 函数用以筛选的相关内容,下面一起来看一下,希望对大家有帮助。

VS Code的确是一款非常热门、有强大用户基础的一款开发工具。本文给大家介绍一下10款高效、好用的插件,能够让原本单薄的VS Code如虎添翼,开发效率顿时提升到一个新的阶段。

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于数据类型之字符串、数字的相关问题,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于numpy模块的相关问题,Numpy是Numerical Python extensions的缩写,字面意思是Python数值计算扩展,下面一起来看一下,希望对大家有帮助。

pythn的中文意思是巨蟒、蟒蛇。1989年圣诞节期间,Guido van Rossum在家闲的没事干,为了跟朋友庆祝圣诞节,决定发明一种全新的脚本语言。他很喜欢一个肥皂剧叫Monty Python,所以便把这门语言叫做python。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Dreamweaver Mac version
Visual web development tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Atom editor mac version download
The most popular open source editor

Notepad++7.3.1
Easy-to-use and free code editor
