Today we share how to make a good-looking heart-shaped photo wall with python for your reference, as follows
effect
Let's take a look at the rendering to get an idea of what we're going to do next, which I have below:
How do you feel? If you're still happy with it, read it and have the honor of clicking the like button, because after reading it you can make it too! Also it's a kind of feedback for me
reasoning
On the code before, let's first clear the idea, only the process is clear, in order to get their desired results
The most difficult part of making this I think lies in how to calculate the position of each picture to make a heart shape. But it's okay, we do not go to the calculation, after all, I'm very poor in math, I use the earth's way, directly write it position dead, although slightly troublesome, but flexible ~!
We all know excel it, inside the table is a combination of cells, then please also imagine the above picture into a table, is composed of many cells, the size of the cell is the size of the picture, and remove the border. Then make this heart, just add a single picture in the cell, and then put together the shape of the heart is completed. This is the overall idea, the next code to realize this idea
intend
I'll start by saying which python libraries I'll be using for the production process, the tools always need to be in place first, or else I'll get a der~
1、pillow:used to deal with the picture library, belongs to the third-party library, you need to install additional pip install pillow
2, os: used to get the picture, python comes with, do not need to install additional
3, math: used to calculate the number of pictures, python comes with, do not need to install additional
With the above three libraries you are ready to take on the code and go!
coding
1. Introduction to the library
from PIL import Image import os import math
2、Making shapes
We determine the position of the picture by means of a two-dimensional array, where each element is equivalent to a cell in excel: 1 for a blank space and 0 for the picture. See the heart made of 0? Here pure hand-typed (so you are free to change, make the shape you want, this is the flexible place ~)
Since I have 500 images, I want to make it bigger and put more images, so the number of elements will be more, here is the size of 21x21.
Remember: the number of zeros must be less than or just equal to the number of pictures you have, otherwise it won't be a complete heart!
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, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1], [1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1], [1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1], [1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1], [1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1], [1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1], [1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1], [1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 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, 1, 1, 1, 1], ]
3、Import pictures
With the location set, start getting all the images
# This is the path to my picture img_dir = r"C:\Users\admin\Desktop\ avatar" # Get all files under this file, don't put non-image files since there is no filter for other types of files # The return is a list imgs = (img_dir) # Set the size of the picture, all picture sizes should be kept uniform, be organized and disciplined img_h = img_w = 192 # Calculate the number of rows, i.e. the number of sublists rows = len(map) # Calculate the number of columns, i.e. the number of elements in the sub-list columns = len(map[0])
picture (e.g. of life in the city)
The next step is to prepare a white background to be used as a canvas. With the number of rows and columns and the width and height of the image, you can calculate the size of the entire canvas
# The first parameter is written as it is # The second parameter needs to be passed a tuple, the first parameter of the tuple is the width of the canvas and the second is the height # The third parameter is passed the color of the canvas # Create a canvas using the () method figure = ("RGB", (img_w*columns, img_h*rows),"white")
So far, the picture and the canvas are ready, now it's time to place the picture on the corresponding position of the canvas, i.e., the position where the element of the array is 0
# Indicates the subscript of the image count = 0 # Traversing rows for i in range(len(map)): # Iterate over all elements in each row for j in range(len(map[i])): # If the element is 1, leave it alone if map[i][j] == 1: continue # If the element is non-1, i.e. 0 put a picture on it else: # Make an exception to prevent some images from failing to open, causing the program to break. try: # Get the image object using the ("image path") method image = ((img_dir, imgs[count])) except: continue # resize((new width, new height)) is used to change the size of the image, receive a tuple image = ((img_w, img_h)) # Paste the modified image onto the canvas (figure). # The first parameter is a picture object # The second parameter is the position of the image on the canvas, which corresponds to the position of the cell (image, (img_w*j, img_h*i)) # When you're done using an image, write it down and move on to the next image count += 1 # When the loop ends, it means that the heart photo map is complete # Display the painted canvas for you to see # () # Dissatisfied in adjusting the position of 0 in the two-dimensional array, satisfied that you can save it locally and use it for pretending # Need to tell the program the path where the image is saved ('Heart-shaped photo wall.png')
To this point, the heart-shaped photo wall on the production of out, if you have a girlfriend, you can use their girlfriend's photo, the background with red, and then sent to your girlfriend to see, will not be more love you I do not know, you can also write a i love you ~ (Figure below). I don't have one, I'm so angry!
This is the whole content of this article.