Write a Qixi confession artifact in Python

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.

Love Tree

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)

Confession balloon

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)

Confession Card

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

Write a Qixi confession artifact in Python
How to write a confession program in python
Write gui in python
How to write classes in python
How to write return in python
How to write win programs in python
How to write try statement in python
What is a sequence table in Python
Is a number in python a variable type
How to sort a dictionary in python
Is there a helper function in python
Functions in python
How to simulate gravity in a Python game
Install Python3 environment in a brand new Ubuntu
python-Use python to write a small shopping program
Teach you how to write games in python
Python write Tetris
How to understand a list of numbers in python
How to create a Python virtual environment in Ubuntu 14.04
Python implements FTP to upload files in a loop
03. Operators in Python entry
Join function in Python
12. Network Programming in Python3
print statement in python
Concurrent requests in Python
Install python in Ubuntu
Context management in Python
Arithmetic operators in python
MongoDB usage in Python
Str string in Python
Computational Geometry in Python
How to find the area of a circle in python
A large inventory of commonly used third-party libraries in Python