#-*-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 ') |