Home > Backend Development > Python Tutorial > Code examples for implementing emoticon packages in Python

Code examples for implementing emoticon packages in Python

Y2J
Release: 2017-05-16 11:58:52
Original
5155 people have browsed it

This article mainly introduces the method of using Python for simple image processing and the example of Python automatically producing expression packs. It has a very good reference value. Let’s take a look at it with the editor.

As a data analysis Teachers should believe in a 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 only 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 emoticon 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 we just mechanically import pictures into PS - change the text - and export, so we still keep these very low mechanized repetitive actions. Let the program do it.

I always thought that automatic image processing 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!

(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.

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()
Copy after login

In this way you will see the synthesized picture:

There is a blank space below this picture, which is for us to add text in the last step. of. 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")
Copy after login

In this way, a complete emoticon package is generated:

draw.text() is to input text into the layer, so you can Execute this command repeatedly 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! Please think about this question:

  • The white space under the emoticon package is limited

  • The space occupied by Chinese, English and punctuation marks Different

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

Therefore, When 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 do you design an algorithm to find the appropriate font size, text insertion position, and where to wrap the text so that the text Can it be displayed in the center of a limited space and satisfy a certain look and feel?

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

So, through this serious tutorial, we learned how to use Python for simple image processing, and even got a practical 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...

[Related recommendations]

1. Special recommendation: "php Programmer Toolbox" V0.1 version download

2. Python Free video tutorial

3. Python object-oriented video tutorial

The above is the detailed content of Code examples for implementing emoticon packages in Python. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template