Today is the Qixi Festival. Compared with the 502 created by modern people, it is not 520. Qixi is Valentine's Day in the traditional Chinese sense. This article shares a few Python confession programs. Couples can learn and use them now, or if they are single Collect it, maybe you will use it next time.
First, let's draw a tree full of fruits of love.
The main implementation code:
# Draw love
def love(x, y):
lv = turtle.Turtle()
lv.hideturtle()
lv.up()
# Positioning
lv.goto(x, y)
# Draw arc
def curvemove():for i inrange(20):
lv.right(10)
lv.forward(2)
lv.color('red','pink')
lv.speed(10000000)
lv.pensize(1)
lv.down()
lv.begin_fill()
lv.left(140)
lv.forward(22)curvemove()
lv.left(120)curvemove()
lv.forward(22)
# Reset after drawing
lv.left(140)
lv.end_fill()
# Draw tree
def tree(branchLen, t):
# Too few remaining branches to end the recursion
if branchLen >5:
# If the remaining length of the branch is short, it turns green
if branchLen <20:
t.color("green")
t.pensize(random.uniform((branchLen +5)/4-2,(branchLen +6)/4+5))
t.down()
t.forward(branchLen)love(t.xcor(), t.ycor())
t.up()
t.backward(branchLen)
t.color("brown")return
t.pensize(random.uniform((branchLen +5)/4-2,(branchLen +6)/4+5))
t.down()
t.forward(branchLen)
# The following recursion
ang = random.uniform(15,45)
t.right(ang)
# Decrease the length randomly
tree(branchLen - random.uniform(12,16), t)
t.left(2* ang)
# Decrease the length randomly
tree(branchLen - random.uniform(12,16), t)
t.right(ang)
t.up()
t.backward(branchLen)
Let's take a look at the realization of the confession balloon. The effect to be achieved is: randomly generate balloons of various colors floating upwards, and click on the balloons to break.
The main implementation code is as follows:
# balloon
balloons =[]
# colour
color_option =["red","blue","green","purple","pink","yellow","orange"]
# Balloon size
size =50
# Balloon line
def line(x, y, a, b, line_width=1, color_name="black"):up()goto(x, y)down()color(color_name)width(line_width)goto(a, b)
def distance(x, y, a, b):
# Determine the distance between the mouse click position and the balloon coordinates
return((a - x)**2+(b - y)**2)**0.5
def tap(x, y):for i inrange(len(balloons)):
# Determine whether to click one of the balloons in the queue
ifdistance(x, y, balloons[i][0], balloons[i][1])<(size /2):
# Delete balloon
balloons.pop(i)return
def draw():
# Clear canvas
clear()for i inrange(1,(len(balloons)+1)):line(balloons[-i][0], balloons[-i][1], balloons[-i][0], balloons[-i][1]- size *1.5,1)up()goto(balloons[-i][0], balloons[-i][1])
# Draw the origin, the parameters are size and color
dot(size, balloons[-i][2])
# Change the ordinate to imitate the rising of a balloon
balloons[-i][1]= balloons[-i][1]+1
# Modify the canvas
update()
def gameLoop():
# 1 /50 probability to generate a balloon
ifrandrange(0,50)==1:
# Balloon coordinates, minus the balloon size at the border position
x =randrange(-200+ size,200- size)
# Randomly select a color in the color queue
c =choice(color_option)
# Add balloon queue
balloons.append([x,-200- size, c])draw()ontimer(gameLoop,10)
We can use Python to add some poems suitable for the theme to the original photos to make confession cards.
Original image:
Effect picture:
The main implementation code is as follows:
img = cv2.imread('test.png')
mask = np.zeros(img.shape[:2], np.uint8)
size =(1,65)
bgd = np.zeros(size, np.float64)
fgd = np.zeros(size, np.float64)
rect =(1,1, img.shape[1], img.shape[0])
cv2.grabCut(img, mask, rect, bgd, fgd,10, cv2.GC_INIT_WITH_RECT)
mask2 = np.where((mask ==2)|(mask ==0),1,255)
img = img.astype(np.int32)
img *= mask2[:,:, np.newaxis]
img[img>255]=255
img =img.astype(np.uint8)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
img = Image.fromarray(img,'RGB')
img.save('test1.jpg')
fp =open(r"word.txt","r", encoding="utf-8")
text = fp.read()
mask_pic=np.array(Image.open(r"test1.jpg"))
wordcloud =WordCloud(font_path='hyr3gjm.ttf',mask=mask_pic,max_words=200).generate(text)
image=wordcloud.to_image()
image.save("wordcloud2.png")
cloud_data = np.array(image)
alpha = np.copy(cloud_data[:,:,0])
alpha[alpha>0]=255
new_image = Image.fromarray(np.dstack((cloud_data, alpha)))
card = Image.open("test.png")
card = card.convert("RGBA")
card.paste(new_image,(0,0), mask=new_image)
card.save("card.png")
Recommended Posts