Five interesting Python hand training projects, with code


Python is not only easier to use than other programming languages. In life, the technical scope of Python may not be as deep as that of other programming languages, but Python is definitely the most grounded. In theory, Python's affinity to the people can also be said to be the position of big brother. So what interesting little functions can Python use in our life?


1, Word cloud picture



Do you often see this kind of picture on the Internet? In fact, the official name of this kind of picture is Ci yuntu. The concept of "Word Cloud" was first put forward by Rich Gordon, associate professor of Journalism and director of new media at Northwestern University. Word Cloud is a visual presentation of text data. It generally consists of some color graphics composed of words extracted from text data. The core of Word Cloud map is to convey the valuable information behind a large amount of text data with the visual expression of high-frequency keywords, which is also completed by Python.


Word cloud Code:

def wordCloudImage(wordlist,width,height,bgcolor,savepath):

# You can open your favorite word cloud to show the background map

# cloud_mask = np.array('nezha.png'))

# Define some attributes of word cloud

wc = WordCloud(

width=width, # Sheet width 900

height=height, # Sheet height 3000

background_color=bgcolor, # Split to "black background color"

# mask=cloud_mask, # Background pattern

max_words=300, # Displays the maximum number of words

font_path='./fonts/simhei.ttf', # Display Chinese


# min_font_size=5, # Minimum size

# max_font_size=100, # Maximum size


# wordfile is a list of words after word segmentation

x = wc.generate(wordlist)

# Generate word cloud image

image = x.to_image()

# Show word cloud pictures

# savepath is the image saving address, which saves the word cloud image



2, Generate hand drawn votes


Now many software can realize the technology of converting photos into hand drawn form, but some software will charge. Here, we can also use python. The advantage of using Python is that it has stronger personality and can be customized; And batch conversion can be realized, which is convenient and fast.

Here we use the pilot library, which is a very powerful and professional Python image processing library


Original drawing:


Hand drawing:



# -*- coding: UTF-8 -*-

from PIL import Image

import numpy as np

# Original picture path

original_image_path = "E:\\picture\\Lujiazui.jpg"

# The path of the hand drawn picture to be generated can be customized

handdrawn_image_path = "E:\\picture\\Lujiazui-Hand drawn.jpg"

# Load the original image and convert the image into array data



#Take the gradient value of image gray


#Take the gradient value of horizontal and vertical image








#The top view angle of the light source is converted to radian values


#The azimuth angle of the light source is converted to radian value


#Influence of light source on x-axis




#Normalize the light source and convert the gradient into gray


#Avoid data out of bounds and cut the generated gray value to 0-255


#Image reconstruction




3, Generate art QR code


Now there are many QR code generation tools. python also has a QR code generation library myqr, which can add picture background to the QR code. It looks very interesting. The effects are as follows:



myqr -p d:\hmbb.jpg -c


4, Generate ID photo


Here, we use two sets of codes: pilot and removebg, which are used to modify the photo size and matting respectively.

AI technology is used in removebg here. The matting edge is very soft and the effect is very good.




# encoding=utf-8

from PIL import Image

from removebg import RemoveBg

# removebg involves APIs_ Key, you need to apply on its official website

api_key = 'PysKLJueeoyK9NbJXXXXXXXXX'

def change_bgcolor(file_in, file_out, api_key, color):


#Must be in png format


p, s = file_in.split(".")

rmbg = RemoveBg(api_key, 'error.log')


file_no_bg = "{}.{}_no_bg.{}".format(p, s, s)

no_bg_image =

x, y = no_bg_image.size

new_image ='RGBA', no_bg_image.size, color=color)

new_image.paste(no_bg_image, (0, 0, x, y), no_bg_image)

# Modify photo size

def change_size(file_in, file_out, width, height):

image =

resized_image = image.resize((width, height), Image.ANTIALIAS)

if __name__ == "__main__":

file_in = 'E:\\girl.png'

file_out = 'E:\\girl_cutout.png'

# The size can be modified as required

# change_size(file_in, file_out, width, height)

# Change background color

color = (0, 125, 255)

change_bgcolor(file_in, file_out, api_key, color)


5, Generate Jiugong grid picture


For some time, Jiugongge pictures were popular in the circle of friends, that is, one picture was divided into nine pictures, which quickly stood out in the circle of friends and looked particularly artistic and advanced. This Jiugong grid can also be done with many software. If it is implemented in Python, it can be drafted in less than 50 lines of code.



# Circle of friends Jiugongge picture making

# encoding=utf-8

from PIL import Image

import sys

# First fill the input image with a square

def fill_image(image):

width, height = image.size

# Select the larger value of the length and width of the original picture as the radius of the Jiugong grid of the new picture

new_image_length = width if width > height else height

# Production of new pictures [white background]

new_image =, (new_image_length, new_image_length), color='white')

# Paste the original image on the new image, and the position is centered

if width > height:

new_image.paste(image, (0, int((new_image_length - height) / 2)))


new_image.paste(image, (int((new_image_length - width) / 2), 0))

return new_image

# Cut the picture into nine squares

def cut_image(image):

width, height = image.size

# Three pictures in a row

item_width = int(width / 3)

box_list = []

for i in range(0, 3):

for j in range(0, 3):

box = (j * item_width, i * item_width, (j + 1) * item_width, (i + 1) * item_width)


image_list = [image.crop(box) for box in box_list]

return image_list

# Save picture

def save_images(image_list):

index = 1

for image in image_list:'e:\\picture\\'+str(index) + '.png', 'PNG')

index += 1

if __name__ == '__main__':

file_path = "e:\\picture\\Totoro.jpg"

image =


image = fill_image(image)

image_list = cut_image(image)




Here I would like to recommend my own Python learning Q group: 249029188. All of them are Python learners. If you want to learn or are learning python, you are welcome to join. Everyone is a software development party and shares dry goods from time to time (only related to Python software development), including a copy of the latest Python advanced materials and zero basic teaching in 2021 compiled by myself, Welcome to advanced and interested partners of Python!

Keywords: Python Programmer AI Data Analysis

Added by cowboy_x on Fri, 18 Feb 2022 19:34:11 +0200