import random # #Random module, can generate random numbers from PIL import Image,ImageDraw,ImageFont,ImageFilter ''' PIL needs to be installed via pip install PIL Image is responsible for image processing ImageDraw is responsible for handling brushes ImageFont is responsible for handling fonts ImageFilter is responsible for processing filters ''' #Project ideas: # 1. Define a picture img = Image.new( " RGB " ,(150,50),(255,255,255 )) """ The first parameter: represents the color we use The second parameter: represents the image size The third parameter: the color of the specific image """ # 2. Create brush draw = ImageDraw.Draw(img) # 3. Draw lines and points # draw lines for i in range(random.randint(1,10 )): draw.line( #There is a feature when drawing lines: each line has two points, and each point is determined by the two values of X and y [ (random.randint ( 1,150), random.randint (1,150 )), (random.randint ( 1, 150), random.randint (1, 150 )) ], fill = (0,0,0) ) #Draw points for i in range(1000 ): draw.point( [ random.randint ( 1,150 ), random.randint ( 1,150 ) ], fill = (0,0,0) ) # 4. Draw our text # Our text is random # a certain number # define the random numbers and letters we want to generate font_list = list( " qwertyuiopasdfghjklzxcvbnmQWERTYUIOPLKJHGFDSAMNBVCXZ0123456789 " ) c_chars = "" .join(random.sample(font_list,5 )) #draw font font = ImageFont.truetype( " simsun.ttc " ,32 ) draw.text((5,5),c_chars,font = font,fill = "green") """ The first parameter: represents the position of the text, the distance from the top and the left The second parameter: represents the text and content The third parameter: font and color """ # random.sample is to randomly take out the specified elements in the specified list # 5. Define the parameters of distortion params = [ 1-float(random.randint(1,2))/100 , 0, 0, 0, 1-float(random.randint(1,2))/100, float(random.randint(1,2))/50, 0.001, float(random.randint(1,2))/50 ] # 6. Use filter #Add filter img = img.transform((150,50 ),Image.PERSPECTIVE,params) """ first parameter: the extent of the distortion, The second parameter: the twisted style The third parameter: the parameter of the twist """ #Do distortion img = img.filter(ImageFilter.EDGE_ENHANCE_MORE) #Call img.show ()