Python uses pil to generate an image Verification Code

Source: Internet
Author: User

Python uses pil to generate an image Verification Code

This article mainly introduces how python uses pil to generate Image verification codes. It involves the skills related to Python operations on modules such as Image, ImageDraw, and ImageFont. For more information, see

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

#-*-Coding: UTF-8 -*-

# Import three modules

Import Image, ImageDraw, ImageFont

Import random

Import math

'''Basic function '''

# Image Width

Width = 100

# Image Height

Height = 40

# Background color

Bgcolor = (1, 255,255,255)

# Generating background images

Image = Image. new ('rgb ', (width, height), bgcolor)

# Loading Fonts

Font = ImageFont. truetype ('freesans. ttf', 30)

# Font color

Fontcolor = (0, 0, 0)

# Generate a draw object. draw is a set of algorithms.

Draw = ImageDraw. Draw (image)

# Draw a font. (0, 0) indicates the starting position.

Draw. text (1234), '000000', font = font, fill = fontcolor)

# Release draw

Del draw

# Save the original version

Image.save('1234_1.jpeg ')

'''Demonstrate distortion. You need to create a new image object '''

# New Image

NewImage = Image. new ('rgb ', (width, height), bgcolor)

# Load pixels

NewPix = newImage. load ()

Pix = image. load ()

Offset = 0

For y in range (0, height ):

Offset + = 1

For x in range (0, width ):

# New x coordinate points

Newx = x + offset

# Try the following results:

# Newx = x + math. sin (float (y)/10) * 10

If newx <width:

# Offset the source pixel to the new Pixel

NewPix [newx, y] = pix [x, y]

# Save the distorted version

NewImage.save('1234_2.jpeg ')

'''Deformation '''

# X1 = ax + by + c

# Y1 = dx + ey + f

NewImage = image. transform (width + 30, height + 10), Image. AFFINE, (1,-0.3, 0,-0.1 ))

NewImage.save('1234_3.jpeg ')

'''Draw interference lines. Do not draw too many lines so that users cannot see clearly '''

# Create a draw and draw a line

Draw = ImageDraw. Draw (newImage)

# Line color

Linecolor = (0, 0)

For I in range (0, 15 ):

# All are random.

X1 = random. randint (0, width)

X2 = random. randint (0, width)

Y1 = random. randint (0, height)

Y2 = random. randint (0, height)

Draw. line ([(x1, y1), (x2, y2)], linecolor)

# Save to local

NewImage.save('1234_4.jpeg ')

I hope this article will help you with Python programming.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.