python confession goddess


Tomorrow is Valentine's Day, the program ape who must have been Tucao life a lot, Xiao Bian also often Tucao straight men, not chat, not romantic, and so on. Is not it feel very humbled, I remember when I was done led 8x8x8 person confession artifact. But now because of the impact of the virus, we should not have these side hardware facilities, so today I will share with you how declare to the goddess program ape the way, well we get into.

Goddess Photo

First, we start with the goddess circle of friends borrow a picture of Mimi da, here I tried to get a picture of my goddess (pixels 1920x1200)
Here Insert Picture Description

final effect

Look at the final result of it
Here Insert Picture Description
we will generate photo Click to enlarge (Surprise !!!)
Here Insert Picture Description

Code

In fact, the whole process is not complicated, only a few less than 30 lines of code, the principle is very simple and
we all know that every image is composed of pixels, each pixel has its own color, its color may be an array with a to represent: (a, b, c) , in which each number is the range of 0-255, (0,0,0) representing white, (255,255,255) on behalf of black, that is, we often say that the RGB values
here I emphasize that the use of the best photos a little pixels high, or will be apparent to the naked eye to see the jagged sense
ideas implemented on the word: as long as each pixel out a pixel value, and use this as the color of the pixel that is the word can, in many cases enough amount of pixels, from a distance, we are able to see the outline of the original image.
First, pillow.Image read image, and a function obtained using the load value of each pixel, this module may not be installed at

img_raw = Image.open(img_path)
img_array = img_raw.load()

Then create a canvas, and choose the right font and font size you want to use

#字体颜色
img_new = Image.new("RGB", img_raw.size, (0, 0, 0))
draw = ImageDraw.Draw(img_new)
#字体,可以使用windows系统自带的
font = ImageFont.truetype('C:/Windows/fonts/Dengl.ttf', font_size)

Here fonts can actually use the system comes fonts are in this path: C: / Windows / fonts /
Here Insert Picture Description
fonts can be selected
and then the need to continue the cycle, "I love you!", These five characters (strings can customize your own). Herein can be implemented using a while loop generator yield

def character_generator(text):
    while True:
        for i in range(len(text)):
            yield text[i]

Next, we give these words with the corresponding color, written in the newly created canvas

for y in range(0, img_raw.size[1], font_size):
   for x in range(0, img_raw.size[0], font_size):
       draw.text((x, y), next(ch_gen), font=font, fill=img_array[x, y], direction=None)

Finally, we generated the preservation picture

img_new.convert('RGB').save("output.jpg")

The complete code

from PIL import Image, ImageDraw, ImageFont

font_size = 7
text = "我喜欢你!"
img_path = "input.jpg"

img_raw = Image.open(img_path)
img_array = img_raw.load()

img_new = Image.new("RGB", img_raw.size, (0, 0, 0))
draw = ImageDraw.Draw(img_new)
font = ImageFont.truetype('C:/Windows/fonts/Dengl.ttf', font_size)

def character_generator(text):
    while True:
        for i in range(len(text)):
            yield text[i]

ch_gen = character_generator(text)

for y in range(0, img_raw.size[1], font_size):
    for x in range(0, img_raw.size[0], font_size):
        draw.text((x, y), next(ch_gen), font=font, fill=img_array[x, y], direction=None)

img_new.convert('RGB').save("output.jpg")

Finally, I wish you all Happy Valentine's Day, declare success! ! !

Published 21 original articles · won praise 28 · views 3709

Guess you like

Origin blog.csdn.net/LPJCSY/article/details/104295103