In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "how python achieves the heart-shaped photo wall effect". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to achieve the heart-shaped photo wall effect by python".
Effect.
Let's first take a look at the effect picture to understand what we are going to do next. My effect picture is as follows:
How do you feel? If you are still satisfied, please like it after watching it, because you can also do it after reading it! It is also a kind of feedback to me at the same time.
Train of thought
Before we go to the code, let's sort out the next train of thought. Only when the process is clear, can we get the results we want.
I think the most difficult part of making this is how to calculate the position of each picture in order to form a heart shape. But it doesn't matter, we don't calculate. After all, I'm poor at math. I use the local method to write down its position directly. Although it's a little troublesome, it's flexible.
We all know excel, inside the table is composed of a combination of cells, then please imagine the above picture as a table, is composed of many cells, the size of the cell is the size of the picture, and removed the border. So to make this heart shape, just add a single picture to the cell, and then put together the shape of the heart. This is the overall idea, and then use the code to implement this idea.
Prepare for
Let me first talk about which python libraries should be used in the production process, and the tools must be done well first, or else a der~ will be built.
1. Pillow: a library used to process images, which belongs to a third-party library. Additional installation of pip install pillow is required.
2. Os: used to obtain images. Python comes with it. No additional installation is required.
3. Math: used to calculate the number of images. Python comes with it without additional installation.
It is OK to have the above three libraries, and then the code will go.
Code
1. Guide the library
From PIL import Imageimport osimport math
2. Make the shape
We use a two-dimensional array to determine the location of the picture, and each element is equivalent to the cell in excel: 1 for white space and 0 for picture. Do you see the love made up of zero? Here is pure hand play (so you are free to change and make the shape you want, this is the flexibility ~)
Because I have 500 pictures, so I want to make a little bigger, put more pictures, so the number of elements will be more, the following is the size of 21x21.
Remember: the number of 0 must be less or exactly equal than the number of your pictures, otherwise it will not be a complete love!
Map = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1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, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],]
3. Import pictures
The location is determined, and we start to get all the pictures.
# this is the path of my picture img_dir = r "C:\ Users\ admin\ Desktop\ avatar" # get all the files under this file, because other types of files are not filtered, so do not put non-picture files # what is returned is a list imgs = os.listdir (img_dir) # set the size of the picture, all the picture size should be the same Calculate the number of rows in an organized and disciplined img_h = img_w = 19 rows, that is, the number of sublists rows = len (map) # calculate the number of columns, that is, the number of elements in the sublist columns = len (map [0])
Draw a picture
The next step is to prepare a white background as a canvas. With the number of rows, columns, and the width and height of the picture, the size of the entire canvas can be calculated.
# the first parameter is written according to # the second parameter needs to pass in a tuple, the first parameter of the tuple is the width of the canvas, the second parameter is high # the third parameter is the color of the canvas # use the Image.new () method to create a canvas figure = Image.new ("RGB", (img_w*columns, img_h*rows), "white")
So far, the picture and the canvas are ready, and now it's time to place the picture in the corresponding position on the canvas, that is, the position where the element in the array is 0.
# represents the subscript of the picture count = traverses the line for i in range (len (map)): # iterates through all the elements in each line for j in range (len (map [I])): # if the element is 1, ignore it if map [I] [j] = = 1: continue # if the element is not 1 Else: # do an exception handling to prevent some pictures from failing to open. Causes the program to interrupt try: # use the Image.open ("picture path") method to get the picture object image = Image.open (os.path.join (img_dir, IMGS [count]) except: continue # resize ((new width) (new high)) to change the size of the picture, receive a tuple image = image.resize ((img_w, img_h)) # paste (paste) the modified picture (image) onto the canvas (figure) # the first parameter is the picture object # the second parameter is the position of the picture on the canvas Equivalent to the position of the cell figure.paste (image, (img_w*j, img_h*i)) # record it after using a picture, and start using the next picture count + = 1 # when the cycle ends, it means that the heart-shaped picture has been completed # display the painted canvas Let you see that figure.show () # is not satisfied with adjusting the position of 0 in the two-dimensional array. If you are satisfied, you can save it locally and use it to force # you need to tell the program the path to save the picture figure.save ('heart-shaped photo wall. PNG')
At this point, the heart-shaped photo wall is made. If you have a girlfriend, you can use your girlfriend's picture with a red background, and then send it to your girlfriend to see if you love you more. I don't know. You can also write an I love you~ (figure below). It's a pity I didn't. I'm so angry!
Thank you for your reading, the above is the content of "how to achieve the heart-shaped photo wall effect of python". After the study of this article, I believe you have a deeper understanding of how to achieve the heart-shaped photo wall effect of python, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 219
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.