A very skinny practice Python automatically generates emoticons


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! ! ! !

表情包It is not just a symbol, but also a culture; it is one of the driving forces that promotes social interaction 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!

640?wx_fmt=jpeg&wxfrom=5&wx_lazy=1

(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 on the Internet, with the cute panda head as the background, and add text below to form an emoji package.

640?wx_fmt=jpeg

640?wx_fmt=jpeg

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.

  1. from PIL import Image, ImageDraw, ImageFont

  2. img = Image.open(".ackground.jpg")

  3. jgz = Image.open(".jgz.jpg")

  4. img.paste(jgz,(73,42))

  5. img.show()

This will give you the resulting image:

640?wx_fmt=png

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.

  1. draw = ImageDraw.Draw(img)

  2. ttfront = ImageFont.truetype('simhei.ttf', 24)

  3. draw.text((32, 190),"我的内心毫无波动   甚至还想笑",fill=(0,0,0), font=ttfront)

  4. img.show()

  5. img.save(".Python生成的表情包.jpg")

In this way, a complete expression package is generated:

640?wx_fmt=png

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

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?

640?wx_fmt=png

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

∞∞∞



640?wx_fmt=jpeg&wx_lazy=1&wxfrom=5

IT School - {Technology Youth Circle} continues to pay attention to the fields of Internet, blockchain and artificial intelligence 640?wx_fmt=jpeg&wx_lazy=1&wxfrom=5



The official account replied "Python" ,

Invite you to join {IT send Python technology group} 

Guess you like

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