Valentine's Day coming up, teach you skills in Python confession

Author : @ Domingo
Public number : Python programming time


By 2020, this year looks so romantic, you were a man?

Implying I can be a dog?

18 years time, wrote an article on how to use Python to confession.

Although creativity and the results are good, but there is a drawback, it is that the exe file, the goddess of the need to open the computer, be possible to get involved, then you are successful. "Molested."

Because it is very early in the article, there should be a lot of people have not seen.

Not read, you can click here to see: in Python allows you to write a confession out of a single artifact

Mind you, the day after tomorrow is February 14 a. what? Or a dog do?

Okay, you caught up, today's article is written for you.

Domingo today to teach you how to use Python to tell the truth to the hearts of the goddess.

Some time ago, on the microblogging brush to a recommendation. Content like this

img

Out of curiosity, I opened up, enlarge and then zoom, emmm, a bit mean acridine ...

img

These four words, for people like me who shy DS male, really embarrassed to say, say it, if rejected supposed to?

Use routines to declare and observe each other's reactions, you would be able to understand each other if you have a good impression, at first test they have almost a certainty to start again and perhaps more secure.

Today, we teach a routine like this: how to use Python to do out of such a graph, a little romance, and a little geeky. Can you win the goddess, it is up to you (life) up. (1 • ₃ • 1)

First of all, you have to find one of your goddess high-quality pictures (high resolution as much as possible of it, the effect will be better).

Here I attempt to do it demonstrates a high round, this is the original (resolution: 2000 * 1328).

img

I use a script written after the run, they generate such a map, please click to enlarge and then zoom. (Surprise?

img

This image is then sent to your goddess, concrete, then surgery you want slightly.


Well, compared to the goddess, you might care more about how this is achieved ( You Deserve to Be Single ).

In fact, very simple principle, be less than 20 lines of code.

First of all, to say something about the principle.

In fact, each picture are made one by one pixel composed. And each pixel, has its own color, which color can be represented by an array of: (a, b, c), wherein each number range is 0-255.

For example, (0,0,0) representing white, (255,255,255) black.

When the pixels enough time, this picture is what we call high-definition photos.

If too few pixels when our naked eye can perceive a clear sense of teeth.

Drew a chart in Excel, each square represents a pixel, which if I set the font size 5 (5 non-famous, but each character occupies five pixels), the following effect is probably like this.

Here Insert Picture Description

I just took out a pixel value of each pixel, and use this as a pixel color to the word, and in many cases enough amount of pixels, from a distance, we are able to see the outline of the original image.

With ideas, we can begin our code.

First, pillow.Image read image, and using the load function value obtained for each pixel.

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)
font = ImageFont.truetype('C:/Windows/fonts/Dengl.ttf', font_size)

Because of the need to constantly circulating "I love you!", These five characters. Here it can be achieved a yield while loop generator.

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

Finally, give these words with the corresponding color, writes 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, the finished product will save

img_new.convert('RGB').save("F://gyy_save.jpeg")

The complete code is as follows, for your reference

from PIL import Image, ImageDraw, ImageFont

font_size = 7
text = "我喜欢你!"
img_path = "F://gyy.jpeg"

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("F://save.jpeg")

Finally, on a few more renderings of it (Yes, I am a fan of pirates).

  1. Luffy

img

  1. Sauron

img

  1. Sanji

img


This article first appeared in public my personal [No.]: Python programming time

Upload pictures may be compressed, view the results, go to: Valentine's Day coming up, teach you skills in Python confession

Published 11 original articles · won praise 10 · views 1766

Guess you like

Origin blog.csdn.net/weixin_36338224/article/details/104287582