Detailed explanation of Python's automatic production of table packages

Source: Internet
Author: User
This article describes how to use Python to process simple images and how to use Python to automatically produce table packages, next, let's take a look at the small series. This article mainly introduces the simple image processing method using Python and the example of Python's automatic production of table packages, which has good reference value. let's take a look at it with the small series below.

As a data analyst, we should believe in one sentence-"one image wins thousands of words ". However, what we want to talk about here is not data visualization, but a universal product form-an expression pack !!!!

Table storeIt is not just a symbol, but also a culture-it is one of the motivations to promote social and social development, just like lazy. We insist that a good expression pack should be a piece of work of art, a burst of inspiration from the spring breeze of the night, and a burst of passionate thoughts and thoughts from the ground up, it's the lofty sentiments that go straight to the cloud, it is the glory and victory of all the mountains and small places in the world. it is a kind of straight-line thinking that cannot tolerate code farming. its common forms are fixed, the process is fixed, there is no beauty, there is no artistic change and surprises. However, in the process of producing table packages, it would be too low to mechanically import images into PS-change text-export, so let's hand over these low-level mechanical repetitive actions to the program.

Previously, I thought that the automatic processing of images relies 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. Therefore, for image processing, the complex parts are still manually completed in PS or AI, and simple processing can be completed using Python programs.

Before the start of the project, let's look at the milestones in the expression pack field and thank them for their great efforts in promoting the remarkable progress of the expression pack in the historical trend. their British stars have been enjoyed by the masses for a long time, their voice and smile will survive in people's daily social networking, and their outstanding contributions will always be engraved on the monument to the history of the family!

(If you can accept the copyright fee, you don't have to work hard to play, film, broadcast ......)

Material preparation

Here we are centered on the face of the most widely circulated director Jin, taking the cute PandaTV head as the background, and adding words to the bottom can form a table bag.

Then, use psto process a piece of size, set the template size to 250*250, and crop the white edges.

Image superposition

The first step is to overlay the emoticon to the template, and notice that our emoticon material is white, not transparent, so the position should be properly controlled, otherwise it will cover the outline.


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

Then we can see the merged image:

There is a whitelist at the bottom of this image, which is the last step for us to add text. Come on, the Magic smile has been shown, and the next is the last blow. are you OK ?! Oh no, are you ready ?!

Text superposition

Although the core of an expression pack is an expression, a short and shocking line of text can often play the role of directly hitting the soul.


Draw = ImageDraw. draw (img) ttfront = ImageFont. truetype ('simhei. ttf', 24) draw. text (32,190), "My heart is not fluctuating \ n or even want to laugh", fill = (, 0), font = ttfront) img. show () img. save (". \ forms generated by python.jpg ")

In this way, a complete table package is generated:

Draw. text () is used to input text into the layer. Therefore, you can execute this command to add multiple layers of text to the image.

You can also import multiple emotices and multiple text lines to automatically generate table packages in batches ......

Problem sublimation

Do you think it's over now? Too young! Please think about this problem:

  • The white space in the table bag is limited.

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

  • If the text is too long, you need to wrap it. However, if there are too many lines, the image won't be able to fit.

Therefore,How to design an algorithm to find a proper font size when your mind suddenly shows a line that contains both Chinese and English punctuation characters to enhance the tone, the insertion position of the text. Where does the text wrap?

Forget it. let's end it here ......

So through this serious tutorial, we learned how to use Python for simple image processing, and get to a practical magic: automatic production of table packages.

The most important thing is that the old man often said, "Believe it or not, I will batch create your big pack in batches in minutes", and finally become a reality ......

The above is a detailed description of Python's automatic production of table packages. For more information, see other related articles in the first PHP community!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.