Python Workbook, a small program every day--0000 Questions

Source: Internet
Author: User

Python After the introduction of the need for constant practice to deepen the impression, in order to improve their Python coding ability, specifically to find some Python small examples to practice, this is the original address: https://github.com/Yixiaohan/ Show-me-the-code all the topics are here. Although you can not do one question per day, but will do a time to do an exercise.

Let's start with the first question:


The No. 0000 question: Add your QQ head (or Weibo avatar) to the top right corner with a red number, similar to the amount of unread information that prompt effect. Similar to the effect in the diagram

650) this.width=650; "Src=" https://camo.githubusercontent.com/d518d3929e4054ce2f9183b23e52908da7e5632d/ 687474703a2f2f692e696d6775722e636f6d2f736732646b75592e706e673f31 "alt=" Avatar "style=" Border:0px;background-color: RGB (255,255,255); "/>

Analyze the problem:

Through the analysis of the topic can be learned that the subject is in the picture above the operation, should be two layers, one is QQ Avatar (fixed), the other is a red number (variable), need to use the Python PiL module to deal with the image problem.

Solve the problem:

1 Paste the code First

#!/usr/bin/env python#-*-coding:utf-8-*-from PIL import image,imagedraw,imagefontdef addnum (nub,filepath): img = I Mage.open (filepath) width,height = img.size FontSize = HEIGHT/4 draw = Imagedraw.draw (img) #确定字体格式和字体大小 TtF ont = imagefont.truetype (' Arial.ttf ', fontSize) #确定显示的 position, number, color, font draw.text ((width-fontsize,0), nub, (256,0,0), font=t TFont) del draw Img.save (' qq_addnub.jpg ') img.show () if __name__ = = ' __main__ ': Addnum (' 4 ', ' qq.jpg ')

2 Code parsing

First you need to prepare a picture,

Then, the use of the PIL module image,imagedraw,imagefont Three modules, image is mainly responsible for opening a picture, Imagedraw is responsible for drawing graphics, Imagefont responsible for font type, size, etc.


This article is from "a struggling small operation" blog, please be sure to keep this source http://yucanghai.blog.51cto.com/5260262/1715170

Python Workbook, a small program every day--0000 Questions

Related Article

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.