Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

How dare you resist Xinjiang cotton? Resist you with 50 lines of Python code


May 29, 2021 Article blog


Table of contents


Recently, some foreign clothing brands are making demons, on the one hand, making money for the Chinese people, on the other hand, also issued a so-called statement to resist our great Xinjiang production of high-quality cotton. A s events continue to ferment, a wave of hot searches on Weibo has scum. Many celebrity artists have stopped working with foreign clothing brands.
The editor-in-chief was also very angry at the incident and wanted to boycott the clothing brands in his own way. P rior to the event, many posters or pictures were more or less logos of brands, such as Eason, the king of the music scene, and recently Eason announced the end of its partnership with Aldi. Eason is a global spokesperson and a lifetime contract, this level is very high, can stand up for the first time to lift the cooperation, really praise him!


As a Python enthusiast, I thought I could do something to express my anger with programs, such as coding these brands. If a picture to code can be said to be time-consuming and laborious, small editors through access to relevant information, through a short 50 lines of code can automatically code these logos, let's take a look at it.

01. Procedure explanation

Automated coding of these logos requires two steps:

  • Identify the logo in the image and get the coordinates of the logo
  • Code the logo

02. Identify the logo in the image and get the coordinates of the logo

First, let's look at how to identify brand logos in an image. The recognition here we use Baidu AI recently more popular image recognition application - brand logo recognition.

 How dare you resist Xinjiang cotton? Resist you with 50 lines of Python code1

As you can see, this app recognizes more than 20,000 logos, including logos from some of our most common clothing brands. For image logo recognition, the program is shown in the following image:

 How dare you resist Xinjiang cotton? Resist you with 50 lines of Python code2

The program first reads through the picture in binary mode, encodes base64, then submits the picture and access_token token information to the specified URL, and then returns the logo information, including the logo's brand name and the logo's location in the picture. As shown in the following image:

 How dare you resist Xinjiang cotton? Resist you with 50 lines of Python code3

03. Code the logo

With the coordinate information for the logo, the next step is to code the logo in the picture, and the code program looks like this:

 How dare you resist Xinjiang cotton? Resist you with 50 lines of Python code4

In the above program, first read into the original picture and get the length and width of the picture, and then for the logo area in the picture, according to the pixel value of the logo area are added to the upper-left pixel value of the logo area, the logo area for coding processing, and finally save the coded picture to the local.

Because there may be more than one logo area in a picture, you need to code all the logo areas in a picture through a for loop.


04. Results are shown

Finally, let's take a look at the final results of the code, as shown in the following image.

 How dare you resist Xinjiang cotton? Resist you with 50 lines of Python code5

As you can see, the operation of the logo can be done well by running the program. However, in the process of logo recognition, the presence of light, logo shape changes and occlusion, there may also be logo misdirection and inadequate identification, think of the results in the figure below.

 How dare you resist Xinjiang cotton? Resist you with 50 lines of Python code6

As you can see, the logo in the middle is obscured in the results shown above, resulting in the identification of results and positions that are not particularly accurate, but the logo processing in the upper left corner is excellent.

05. Summary

The above is the small editor today for you to bring small sharing, through the simple fifty-line program processing, you can automatically for the logo mosaic processing. Let's try the effect ourselves and "resist" these clothing brands.

Reprinted from: Rookie Python