20170511 python workbook 0000 Add a red number to the top right corner of the avatar

Source: Internet
Author: User

Now start doing a little Python exercise every day, the first exercise

#!/usr/bin/env python
#-*-Coding:utf8-*-
From PIL import Image, Imagedraw, Imagefont
def add_num (IMG):
Draw = Imagedraw.draw (img) #创建一个可用来对img进行操作的对象, which is to create a drawing object
MyFont = Imagefont.truetype (' C:/windows/fonts/arial.ttf ', size=40) #加载一个TrueType的字体文件, and create a Font object
FillColor = "#ff0000" #填充颜色 >>> Red
width, height = img.size #获取图片的长和宽
Draw.text (width-40, 0), ' The ", Font=myfont, Fill=fillcolor)
#drawObject. Text (position,string,options) to add text inside an image
#Position是一个二元元组, specifies the upper-left coordinate of the string, which is the string to be written
#options选项可以为fill或者font (only one of them can be selected as a third parameter, not two with simultaneous presence, where fill specifies the color of the word,
# font Specifies font and word size, font must be the font type specified in Imagefont, see Imagefont.truetype () for specific usage
Img.save (' result.jpg ', ' jpeg ') #保存图片 and change the format
return 0
if __name__ = = ' __main__ ':
Image = Image.open (' D:\image.jpg ') #打开文件
Add_num (image) #调用函数

Knowledge to be used: reprinted from: http://www.cnblogs.com/denny402/p/5096001.html

First, the opening and display of pictures

From PIL import imageimg=image.open (' D:/dog.png ') img.show ()

Although pillow is used, it is PIL fork, so you still want to import from PiL. Open the picture using the open () function, and use the show () function to display the picture.

This kind of image display is to call the operating system's own image browser to open the picture, sometimes this way is not very convenient, so we can also use another way, let the program to draw pictures.

From PIL import Imageimport matplotlib.pyplot as Pltimg=image.open (' D:/dog.png ') plt.figure ("Dog") Plt.imshow (IMG) Plt.show ()
This approach is more complex, but it is recommended to use a matplotlib library to draw images for display.

Figure by default is with axis, if no need, we can turn off

Plt.axis (' off ')

After you open the picture, you can use some properties to view the picture information, such as

Print img.size  #图片的尺寸print img.mode  #图片的模式print img.format  #图片的格式

The results shown are:

(558, 450) Rgbapng

Second, the preservation of the picture

Img.save (' d:/dog.jpg ')

Just one line of code, very simple. This line of code can not only save the picture, or the conversion format, as in this example, the original PNG image is saved in order to JPG pictures.

Reprinted from: http://www.2cto.com/kf/201603/491448.html

function of the Imagefont module

1. Load

Definition: imagefont.load (file)? Font instance

Meaning: Loads a font from the specified file, and the function returns the corresponding Font object. If the function fails, a IOERROR exception is generated.

2, Load_path

Definition: Imagefont.load_path (file)? Font instance

Meaning: Same as function load (), but if the current path is not specified, the specified font file will be found starting from Sys.path.

3. Truetype

Define 1:imagefont.truetype (file,size)? Font instance

Meaning 1: Load a TrueType or OpenType font file, and create a Font object. This function loads a font object from the specified file and creates a Font object for the font of the specified size.

In a Windows system, if the specified file does not exist, the loader will look for the presence of the Windows font directory.

This function requires a _IMAGINGFT service.

Define 2:imagefont.truetype (File,size, Encoding=value)? Font instance

Meaning 2: (New in 1.1.5) loads a TrueType or OpenType font file and creates a font object with the specified encoding. The usual encoding methods are "UNIC" (Unicode), "Symb" (Microsoft Symbol), "Adob" (Adobe Standard), "ADBE" (Adobe Expert), and "Armn" (Apple Roman).

The following example uses the Microsoftsymbol font, where the variable encoding is "SYMB", and a character is drawn between oxF000 and 0XF0FF.

12 font = ImageFont.truetype("symbol.ttf", 16, encoding="symb")draw.text((0, 0), unichr(0xF000+ 0xAA))

4, Load_default

Definition: Imagefont.load_default ()? Font instance

Meaning: (New in 1.1.4) loads a default font.

Second, the method of Imagefont module

The Font object must implement the following method for use by the Imagedraw layer.

1, GetSize

Definition: font.getsize (text)? (width, height)

Meaning: Returns the width and height of the given text, with a return value of 2 tuples.

2, Getmask

Definition: Font.getmask (text,mode= "")? Image Object

Meaning: Returns a bitmap for the given text. This bitmap is an instance of PIL internal storage memory (defined for the Image.core interface module).

If the font uses antialiasing, the bitmap's mode is "L" and its maximum value is 255. Otherwise, its mode is "1".

The (New in 1.1.5) optional parameter mode is used by some graphics drivers to specify their preferred mode, and if empty, the renderer may return any mode. Note: This pattern is always a string.

Iii. Examples of Imagefont modules

1234567891011121314 >>> from PIL import Image, ImageDraw, ImageFont>>> im02 = Image.open("D:\\Code\\Python\\test\\img\\test02.jpg")>>> draw = ImageDraw.Draw(im02)>>> ft = ImageFont.truetype("C:\\WINDOWS\\Fonts\\SIMYOU.TTF", 20)>>> draw.text((30,30), u"Python图像处理库PIL从入门到精通",font = ft, fill = ‘red‘)>>> ft = ImageFont.truetype("C:\\WINDOWS\\Fonts\\SIMYOU.TTF", 40)>>> draw.text((30,100), u"Python图像处理库PIL从入门到精通",font = ft, fill = ‘green‘)>>> ft = ImageFont.truetype("C:\\WINDOWS\\Fonts\\SIMYOU.TTF", 60)>>> draw.text((30,200), u"Python图像处理库PIL从入门到精通",font = ft, fill = ‘blue‘)>>> ft = ImageFont.truetype("C:\\WINDOWS\\Fonts\\SIMLI.TTF", 40)>>> draw.text((30,300), u"Python图像处理库PIL从入门到精通",font = ft, fill = ‘red‘)>>> ft = ImageFont.truetype("C:\\WINDOWS\\Fonts\\STXINGKA.TTF", 40)>>> draw.text((30,400), u"Python图像处理库PIL从入门到精通",font = ft, fill = ‘yellow‘)>>> im02.show()

Under Windows system, the font files are located under the C:\Windows\Fonts folder. The Simyou.ttf used in this example is a juvenile font file, Simli.ttf script font file, Stxingka.ttf for Xingkai font file. Users can select the desired font file from the Fonts folder, depending on their needs.

The size of the font, which can be set according to the second parameter when defining a font.

The image in this example is im02 as shown:

20170511 python workbook 0000 Add a red number to the top right corner of the avatar

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.