Use python to draw the program code of love, how to use python to draw love to confess

Hello everyone, the editor will answer the questions about the tutorial on how to use Python to draw a love wall with python. Many people still don't know that the confession artifact uses python to draw the love wall code, let's take a look now!

Welcome to add Huawei Cloud Assistant WeChat (WeChat ID: HWCloud002 or HWCloud003 ), enter the keyword " add group " to join the Huawei Cloud online technical discussion group; enter the keyword " Latest Events " to get the latest special promotions from Huawei Cloud. Many HUAWEI CLOUD technical experts and special promotions are waiting for you

It's Qixi Festival again

Today is Qixi Festival, how to use the traditional Chinese Valentine's Day locomotive . It is expected that many young couples have been preparing for this festival for a long time, right? The routine of Bad Street is nothing more than sending flowers, eating, going to the cinema, and finding a hotel after watching the movie. As a special day that detonates consumption, how do programmers celebrate the festival? Today the company blog held a voting event, how to spend Qixi Festival! As a result, most of the people voted behind closed doors and thought about it overnight. No wonder you can't find a girlfriend! Today I will teach you how to send special gifts to her without spending money!

One line of code to draw a heart

I don’t know how many people have been tempted to learn python by this coquettish operation. In fact, if the code is really written like that, it’s better to bring a third-level header when you go to work at night, otherwise it’s easy to suffer from sap. The code is as follows:
print('\n'.join([''.join([('LovePython'[(x-y)%10]if((x*0.05)**2+(y*0.1)**2-1)**3-(x*0.05)**2*(y*0.1)**3<=0 else' ')for x in range(-30,30)])for y in range(15,-15,-1)]))
Effect:

The technology is good, but if you give this as a gift to your girlfriend, unless you don't want to see the sun tomorrow... Today I will give everyone a sharper show operation!

Meituxiuxiu

Now let’s not talk about whether the girl’s mobile phone has beautification software such as Meitu Xiuxiu, even many men’s mobile phones have these software, can you believe it?

Meitu Xiuxiu has a collage function, you can manually select a heart-shaped puzzle, and then paste the pictures you like one by one into the corresponding box, and finally make a heart-shaped picture... So today,
I I will teach you to use Python to draw a heart-shaped girlfriend photo wall!

heart shape layout

On Meitu Xiuxiu, there is a classification of large and small pictures. In order to reduce the difficulty, we always use pictures of equal proportions to draw heart-shaped pictures. First of all, we need to draw a heart-shaped picture, and then classify it according to the small grid.
I used html and css to make a simple heart-shaped picture, let's take a look at the effect first.

Because it is a grid made while thinking, I spelled it out bit by bit with ul tags, and did not use js to dynamically generate it...

I am afraid that the front-end gods will ridicule me, so I will not post the code. It will take up too much content (the download path will be provided with the python code at the end of the article...)
This is a 9X9 div arrangement frame. We only need to fill the red box with the girlfriend's picture, that is It can complete the effect of heart-shaped photo wall. You say you don't have a girlfriend? It doesn't matter, you make a heart-shaped photo wall for the girl you like, maybe you will have it today! Of course, you can also make one for your friends, I am very open-minded, I do not encourage or reject, haha...

code analysis

prepare picture

First of all, you need to sort out some pictures of your girlfriend, this is the main premise! I found 12 photos of my daughter-in-law from WeChat, and saved them in a folder first:

random selection

Someone here wants to ask, count the red squares above, there are 52 in total, and you only have 12 chapters of pictures, how do you do it? According to what you said, if you take 1,000 pictures directly from the photo album, do you have to draw a big radish for you?
We need to introduce the random module, and use random.choicethe list of pictures to dynamically select and fill them. This solves the problem that the number of pictures is not fixed for everyone. You can put thousands of pictures or just one!

picture synthesis

The pillow library was used in the python picture-to-sketch painting written yesterday, and today we can use it to achieve it!

    def mark_pictures(self):
        heart_image = Image.new('RGB', (128 * SideLength, 128 * SideLength))
        row = col = 0
        for side in range(SideLength * SideLength):
            if images_side_calc(col, row):
                img = Image.open(random.choice(self.image_list))
                img = img.resize((128, 128), Image.ANTIALIAS)
            else:
                img = Image.new("RGB", (128, 128), (255, 255, 255))
            heart_image.paste(img, (row * 128, col * 128))
            col += 1
            if col == SideLength:
                col = 0
                row += 1
            if row == col == SideLength:
                break
        heart_image.save("heart_image.jpg")

We first create an empty canvas, and then stipulate that the fixed size of each picture is 128X128, and then we dynamically fill the picture or white curtain according to the calculation of the red box.

As for how to calculate the heart shape, except that the last triangle has rules to follow, the above lines can only be judged individually for each line images_side_calcas follows:

def images_side_calc(row, col):
    if row == 0 and col in [1, 2, 6, 7]:
        return True
    elif row == 1 and col not in [3, 4, 5]:
        return True
    elif row == 2 and col != 4:
        return True
    elif row in [3, 4]:
        return True
    elif row >= 5 and (row - 5) < col < (13 - row):
        return True

The final effect is as follows:

As you can see, since I only used 12 pictures, there will be some randomly repeated pictures. If you use it, you can choose more pictures, and the effect will be better!

Here, I would like to give this Qixi Festival gift to my daughter-in-law who is away from the summer at my sister’s house in Chongqing. I wish you a happy Qixi Festival!

package as exe file

Now that the tool is written, it must be convenient for those who do not have a python environment to use. Use

Now that the tool is written, it is of course convenient for those who do not have a python environment to use. Using the pyinstaller packaging tool does not need to rely on the python environment. Double-click the exe to enter the image storage address to complete the drawing of the heart-shaped photo wall:

Of course, since there is no tkinter GUI configured like yesterday’s sketch, I can only manually enter the path under cmd to complete the production of the photo wall. If you like, you can write a GUI interface by hand according to yesterday’s content.

The End

OK, that's all for today's content. If you think the content is helpful to you, please click "Looking" in the lower right corner of the article.
Reply to the photo wall on the official account, download the packaged exe image-to-sketch tool, let’s have fun together... I
look forward to your attention on my official account 清风Python, if you think it is good, I hope you can move your fingers and forward it to your friends.

Author: Qingfeng Python

Guess you like

Origin blog.csdn.net/mynote/article/details/132424263