Home  >  Article  >  Backend Development  >  Detailed explanation of Python's method of automatically producing emoticon packages

Detailed explanation of Python's method of automatically producing emoticon packages

高洛峰
高洛峰Original
2017-03-19 09:21:401225browse

This article mainly introduces the method of using Python for simple image processing and examples of automatic production of emoticon packages in Python. It has a very good reference value. Let’s take a look with the editor. Come on

As a data analyst, you should believe in the saying - "A picture is worth a thousand words." But what I want to talk about here is not data visualization, but a product form that is popular among all people-emoticon packs! ! ! !

EMoticon is not just a symbol, but also a culture - one of the driving forces to promote socialization and even social development, just like laziness. We insist that an excellent emoji should be a work of art, a sudden burst of inspiration like a spring breeze overnight, a noisy emotional agitation, and an aspiration to sail across the sea. Pride is the small glorious victory that sees all the mountains in the world - it cannot be tolerated by coders using their usual linear thinking deconstruction with fixed forms, fixed processes, no beauty, no artistic changes and surprises. However, in the process of producing emoticons, it would be too low if you just mechanically import the pictures into PS - change the text - and export, so let's leave these very low mechanical repetitive actions to the program. .

I always thought that the automatic processing of images relied on Javascript scripts to control PS or AI; later I discovered that Python can actually do some simple image processing, although it may not be as powerful as MATLAB. Therefore, for image processing, the complex parts are still completed manually in PS or AI, while simple processing can be completed using Python programs.

Before starting work, let us first pay tribute to the milestone figures in the emoji industry and thank them for vigorously promoting the significant progress of emojis in the historical trend. Their names will be talked about by the people for a long time. , their voices and smiles will last forever in people’s daily social interactions, and their outstanding contributions will forever be engraved on the monument of emoticon history!

Detailed explanation of Pythons method of automatically producing emoticon packages

(If royalties could be collected on expressions, then there would be no need to work so hard to play ball, film, or broadcast...)

Material preparation

Here we take the most widely circulated expression of Curator Jin on the Internet as the center, with the cute panda head as the background, and add text underneath to form an emoticon package.

Detailed explanation of Pythons method of automatically producing emoticon packages

Detailed explanation of Pythons method of automatically producing emoticon packages

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

Picture overlay

The first step is to overlay the expression onto the template. Note that our expression material is on a white background rather than transparent, so the position must be controlled well. , otherwise the outline will be covered.


from PIL import Image, ImageDraw, ImageFont
img = Image.open(".\background.jpg")
jgz = Image.open(".\jgz.jpg")
img.paste(jgz,(73,42))
img.show()

You will see the composite picture:

Detailed explanation of Pythons method of automatically producing emoticon packages

There is a blank space below this picture. That's for our final step of adding text. come on, the devilish smile has appeared, and the next step is the final blow, are you OK?! Oh no, are you ready?!

Text Overlay

Although The core of emoticons is expressions, but a short and shocking line of text can often play the finishing touch that directly touches the soul.


draw = ImageDraw.Draw(img) 
ttfront = ImageFont.truetype('simhei.ttf', 24) 
draw.text((32, 190),"我的内心毫无波动\n 甚至还想笑",fill=(0,0,0), font=ttfront) 
img.show()
img.save(".\Python生成的表情包.jpg")

In this way, a complete emoticon package is generated:

Detailed explanation of Pythons method of automatically producing emoticon packages

where draw.text() is Enter text into the layer, so you can continue to execute this command to add multiple layers of text to the image.

You can also import multiple emoticons and multiple text lines, so that you can automatically produce emoticon packages on a large scale...

sublimation of the problem

Do you think it’s over here? Too young! Children, please think about this question:

  • The white space under the emoji package is limited

  • The space occupied by Chinese and English and punctuation marks is different

  • If the text is too long, it needs to be wrapped; but if there are too many lines, the picture cannot fit

So, When a line that contains both Chinese and English and N-number of punctuation marks suddenly flashes in your mind to enhance the tone, how to design an algorithm to Looking for the appropriate font size, text insertion position, and where to wrap the text so that the text can be displayed centered in the limited space and satisfy a certain look and feel?

Detailed explanation of Pythons method of automatically producing emoticon packages

Forget it, let’s end it here...

So, through this serious tutorial, we learned to use Python for The simple image processing method has even led to a practical magical skill: automatically producing emoticons.

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

The above is the detailed content of Detailed explanation of Python's method of automatically producing emoticon packages. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn