background

There are a lot of beautiful pictures in everyone’s mobile phone, and I don’t know how to choose when I send my friend circle every time, how to do?

Kids do multiple choice questions. Make a wall of photos! I want them all!

Like this:

Like this:

And this:

To prepare

To make such a wall, we need to prepare the following:

  • Lots of beautiful pictures
  • Python
  • PIL module

starts

The blogger prepared a group of 110 new friends for operation (in order to save their lives, the blogger did not pose 3000 beautiful women, the principle is the same, comrades can enjoy themselves), below to start building a wall!

Prepare the required modules

from PIL import Image, ImageDraw, PIL import ImageFile ImageFile.LOAD_TRUNCATED_IMAGES = True import OSCopy the code

Set the style of the photo Wall (character)

If we want to construct the photo wall of the specified character, we need to construct the background style of the character first. The construction method is as follows:

Def gen_text_img(text, font_size, font_path=None): "" Enter: text: style the photo wall text font_size: font size font_path: font returns: "Font = imagefont. truetype(font_path, font_size) (width, length) = font.getsize(text) text_img = Image.new('RGBA', (width, Text ((0, 0), text, fill=(0, 0, 0), font=font) return text_imgCopy the code

Set transparency

Next let’s set the transparency. The purpose of setting the transparency is to make the image around the text transparent and look better.

Def trans_alpha(img, pixel): "" R: red G: green B: blue A: transparent" "_, _, _, alpha = img.split() alpha = alpha. Point (lambda I: pixel[-1] * 10) img.putalpha(alpha) return imgCopy the code

replace

The next step is the core operation – image replacement. The main purpose of this step is to replace the blank area when setting the character background wall with the image in our hand.

def picture_wall_mask(text_img, edge_len, pic_dir): Enter: text_img: text image edge_len: photo side length (used to enlarge pixels) pic_dir: New_img = image. new('RGBA', (text_img.size[0] * edge_len, text_img.size[1] * edge_len)) file_list = os.listdir(pic_dir) img_index = 0 for x in range(0, text_img.size[0]): for y in range(0, text_img.size[1]): pixel = text_img.getpixel((x, y)) file_name = file_list[img_index % len(file_list)] try: Img = image.open (os.path.join(pic_dir, file_name)).convert('RGBA') img = img.resize((edge_len, Paste (img, (x * edge_len)) img = trans_alpha(img, pixel) # Y * edge_len)) img_index += 1 except Exception as e: print(f" Failed to open file: {file_name} + {e}") return new_imgCopy the code

The main function

Finally, we put together the various functions prepared above, and we can make the photo wall.

Def main (text = "font_size = 20, edge_len = 60, pic_dir =". / WeChat head ", out_dir = ". / out ", font_path = 'buzz_cloud_font. The vera.ttf') : Font size: param edge_len: sub picture's egde length "" if len(text) >= 1: text_ = ' '.join(text) print(f"generate text wall for '{text_}' with picture path:{pic_dir}") text_img = gen_text_img(text_, font_size, font_path) img_ascii = picture_wall_mask(text_img, edge_len, pic_dir) img_ascii.save(out_dir + os.path.sep + ''.join(text) + '.png') main(text='CA')Copy the code

You’re done

Here’s to all the new friends! Salute!!!

Many of my friends sent me private messages asking me about learning Python. For easy communication, click on blue to join yourselfDiscussion solution resource base