Python automatically generates emoticons, python is in hand, and Doutu is invincible from now on

As a data analyst, you should believe in a saying - "a picture is worth a thousand words". However, what I want to talk about here is not data visualization, but a product form that is popular with all people - emoticons! ! ! !

Emoji is not only a symbol, but also a culture; it is one of the driving forces to promote social and even social development, just like laziness.

We insist that a good emoji should be a work of art, the burst of inspiration that suddenly comes in the spring breeze, the turbulence of the noisy and miscellaneous bombs, and the ambition to hang on the clouds and help the sea Pride is the glorious victory of the small mountains in the world. It cannot be tolerated by the code farmers with their usual fixed forms, fixed processes, no aesthetics, no artistic changes and surprises. Linear thinking deconstruction. However, in the process of producing emoticons, if you just mechanically import the pictures into PS --> change the text --> export, that's too low, so we still hand over these very low mechanized repetitive actions to the program. do it.

I always thought that the automatic processing of images depends on JavaScript scripts to control PS or AI; later I found that Python can actually do some simple image processing, although it may not be as powerful as MATLAB. So for image processing, complex parts are still done manually in PS or AI, and simple processing can be done with Python programs.

Before starting the construction, let us pay tribute to the landmark figures in the emoji industry, and thank them for vigorously promoting the remarkable progress of emoji in the historical trend. Enduring in people's daily social interactions, their outstanding contributions will always be engraved on the monument of the history of expression packs!

Python automatically generates emoticons

(If the expression can charge royalties, then you don't have to work hard to play, shoot, broadcast...)

material preparation

Here we focus on the most widely circulated 金馆长emoji , with the cute panda head as the background, and add text below to form an emoji package.

Python automatically generates emoticons

Python automatically generates emoticons

Then use PS to process the image size, set the template size to 250*250, and crop the white edge of the expression.

image overlay

The first step is to superimpose the expression on the template, notice that our expression material is white instead of transparent, so the position should be controlled well, otherwise it will cover the outline.

from
 PIL import Image, ImageDraw, ImageFontimg =
Image.open(".ackground.jpg")jgz =
Image.open(".jgz.jpg")img.paste(jgz,(73,42))img.show()

This will give you the resulting image:

Python automatically generates emoticons

There is also a blank space below this image, which is for our final step to add text. come on, the magical smile has appeared, the next is the final blow, are you OK?! Oh no, are you ready?!

text overlay

Although the core of the emoji package is the expression, a short and shocking line of text can often play the finishing touch to the soul.

draw
 = ImageDraw.Draw(img)ttfront = ImageFont.truetype('simhei.ttf',
24)draw.text((32, 190),"I have no fluctuations in my heart and even want to laugh",fill=(0,0,0),
font=ttfront)img.show()img.save(".Python generated emoticon package.jpg")

In this way, a complete expression package is generated:

Python automatically generates emoticons

Among them draw.text()is to enter text into the layer, so you can continuously execute this command to add multiple layers of text to the picture.

You can also import multiple expressions, as well as multiple text lines, so that you can automatically produce expression packs in large batches...

problem sublimation

Did you think it was over here? too young! Children's shoes, please think about this question:

  • The blank space under the emoji is limited

  • Chinese and English and punctuation take up different space

  • If the text is too long, it needs to wrap; but if there are too many lines, the picture will not fit

Therefore, when a line that contains both Chinese and English and N multiple punctuation marks to enhance the tone suddenly flashes in your mind, how to design an algorithm to find the appropriate font size, text insertion position, and where to wrap the text , so that the text can be displayed in the center of the limited space, and also meet a certain look and feel?

Python automatically generates emoticons

Well, let's end here...

So, through this serious tutorial, we learned how to use Python for simple image processing, and got a practical magic skill: automatic production of expression packs.

The most important thing is that what the old man often said, "Believe it or not, I will make a lot of your emoticons in batches in minutes" has finally become a reality...

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326053346&siteId=291194637