Python heart photo wall

Posted by shage on Fri, 24 Dec 2021 04:25:54 +0100

ah

Tanabata, Tanabata is coming

A handsome man loses his hair

Why do you lose your hair

I don't know what to give

Straight man's gift was scolded by her

They were beaten randomly

It's good to ask Taobao for help

The accused straight man didn't have to run

To sum up, just one sentence

Single is no trouble

There are thousands of straight men in the world

Half of them have sent photos to the wall

It's usually black and white

In line with the principle of getting up from where you fall

I must insist on sharing it with you

Shine!

Film!

Wall!

This blog post is the PLUS version of the previous one

Click here

Expected results:

The effect is almost the same, aunt:

1. The small square with many old noses forms a big square, and each square is a photo.

2. The surroundings are all black and white, with some messy colors in the middle, which can be assembled into a known mathematical figure, such as circle, square and rectangle.

3. If you are a little nostalgic for this sour love, I suggest you make a heart shape.

1. Script directory

Store photos in pictures

If you have a girlfriend, put some girlfriends

If you don't have a girlfriend, you can put some of the same kind

2. Analysis:

Each photo is different in size and shape. Use the resize method in image to convert to the same size

img = img.resize((SIZE * FRAME[y][x], SIZE * FRAME[y][x]), Image.ANTIALIAS)

Create a two-dimensional array in advance. Different numbers represent different meanings. In the following code, 0 represents a black-and-white photo, 9 represents a space occupation, 1 represents a color photo in this position, similarly, 2 represents a color photo in the surrounding 2 * 2 space. If 2 is used, 9 is required for the remaining three positions in the 2 * 2 square# Set picture matrix

FRAME = [[0,0,0,0,0,0,0,0,0,0,0,0,0,0],         [0,0,0,1,1,0,0,0,0,2,0,0,0,0],         [0,0,1,1,1,1,0,0,1,0,0,1,0,0],         [0,1,2,9,1,1,1,1,1,1,1,1,1,0],         [0,1,9,9,1,4,9,9,9,1,1,1,1,0],         [0,1,1,1,1,9,9,9,9,1,1,1,1,0],         [0,0,1,1,1,9,9,9,9,2,9,1,0,0],         [0,0,0,1,1,9,9,9,9,9,9,0,0,0],         [0,0,0,0,1,1,1,1,1,1,0,0,0,0],         [0,0,0,0,0,1,1,1,1,0,0,0,0,0],         [0,0,0,0,0,0,1,1,0,0,0,0,0,0],         [0,0,0,0,0,0,0,0,0,0,0,0,0,0]]

Taking the upper matrix as an example, the element in the third column of the fourth row is 2, which means that there is a photo in the 2 * 2 square on the right and lower sides. So use 3 9s to fill around 2.

3. For the digital processing of 0-9, only 0 and 9 need special processing, and the rest remain unchanged

if FRAME[y][x] == 9 :            continue        elif FRAME[y][x] != 0 :            pos_x = x * N * SIZE  # Fill start X Coordinate position            pos_y = y * N * SIZE  # Fill start Y Coordinate position            for yy in range(N):                for xx in range(N):                    img = Image.open(PICTURE_DIR + filenames[i])                    img = img.resize((SIZE * FRAME[y][x], SIZE * FRAME[y][x]), Image.ANTIALIAS)                    img_bg.paste(img, (pos_x + xx * SIZE, pos_y + yy * SIZE))                    i += 1        else :            pos_x = x * N * SIZE  # Fill start X Coordinate position            pos_y = y * N * SIZE  # Filling start Y coordinate position for YY in range (n): for XX in range (n): IMG = image open(PICTURE_DIR + filenames[i])                    length = 1 if FRAME[Y][x] == 0 else FRAME[Y][x]                    img = img. resize((SIZE * length, SIZE * length), Image. ANTIALIAS)                    img = img. convert('L')                    img_ bg. paste(img, (pos_x + xx * SIZE, pos_Y + yy * SIZE))                    i += 1

4. All source code

# -*- encoding:utf-8 -*-
 
 
import os
import random
import PIL.Image as Image
import numpy as np
 
# Set picture matrix
FRAME = [[0,0,0,0,0,0,0,0,0,0,0,0,0,0],
         [0,0,0,1,1,0,0,0,0,2,0,0,0,0],
         [0,0,1,1,1,1,0,0,1,0,0,1,0,0],
         [0,1,2,9,1,1,1,1,1,1,1,1,1,0],
         [0,1,9,9,1,4,9,9,9,1,1,1,1,0],
         [0,1,1,1,1,9,9,9,9,1,1,1,1,0],
         [0,0,1,1,1,9,9,9,9,2,9,1,0,0],
         [0,0,0,1,1,9,9,9,9,9,9,0,0,0],
         [0,0,0,0,1,1,1,1,1,1,0,0,0,0],
         [0,0,0,0,0,1,1,1,1,0,0,0,0,0],
         [0,0,0,0,0,0,1,1,0,0,0,0,0,0],
         [0,0,0,0,0,0,0,0,0,0,0,0,0,0]]
         
PICTURE_DIR = "C:/Users/zorrolzhang/Desktop/xxx/pictures/"
 
# Define relevant parameters
SIZE = 100 # The larger the size of each picture, the clearer it is
N = 1     # Place 1 * 1 picture on each point
 
# Calculate relevant parameters
width = np.shape(FRAME)[1]*N*SIZE  # Photo wall width
height = np.shape(FRAME)[0]*N*SIZE # Photo wall height
#n_img = np.sum(FRAME)*(N**2)       # Number of photos required for photo wall
n_img = np.shape(FRAME)[1] * np.shape(FRAME)[0]*N
print(n_img,len(os.listdir(PICTURE_DIR)))
filenames = random.sample(os.listdir(PICTURE_DIR),n_img) # Random selection n_img photos
 
# Draw love wall
img_bg = Image.new('RGB', (width, height))  # Set photo wall background
i = 0
for y in range(np.shape(FRAME)[0]):
    for x in range(np.shape(FRAME)[1]):
        if FRAME[y][x] == 9 :
            continue
        elif FRAME[y][x] != 0 :
            pos_x = x * N * SIZE  # Filling start X coordinate position
            pos_y = y * N * SIZE  # Filling start Y coordinate position
            for yy in range(N):
                for xx in range(N):
                    img = Image.open(PICTURE_DIR + filenames[i])
                    img = img.resize((SIZE * FRAME[y][x], SIZE * FRAME[y][x]), Image.ANTIALIAS)
                    img_bg.paste(img, (pos_x + xx * SIZE, pos_y + yy * SIZE))
                    i += 1
        else :
            pos_x = x * N * SIZE  # Filling start X coordinate position
            pos_y = y * N * SIZE  # Filling start Y coordinate position
            for yy in range(N):
                for xx in range(N):
                    img = Image.open(PICTURE_DIR + filenames[i])
                    length = 1 if FRAME[y][x] == 0 else FRAME[y][x]
                    img = img.resize((SIZE * length, SIZE * length), Image.ANTIALIAS)
                    img = img.convert('L')
                    img_bg.paste(img, (pos_x + xx * SIZE, pos_y + yy * SIZE))
                    i += 1
 
# Save picture
img_bg.save('xxx.jpg')​​​​​​​

5. Running screenshot

6. Write at the end

If you send this picture

I haven't broken up yet

Just be nice to your girlfriend

After all, find someone with bad eyes

Not easy!

Topics: Python