Please indicate the source for reprint: http://blog.csdn.net/jinixin/article/details/79115782
Regarding drawing, Python has many excellent third-party libraries that can support image manipulation, such as Matplotlib, Pillow, etc. These libraries can do various modifications to a given image, or even create new images directly. Today I will try to use Pillow to make a picture with a transparent background and write a sentence on the picture.
module
In the drawing process, I mainly use three modules of Pillow's Image, ImageDraw, and ImageFont. The following is a brief introduction to them:
Image: Constructs an image object, you can create it with new or by loading from an existing file
ImageDraw: Provides simple flat graphics for image objects. I understand it as a brush, which can be manipulated to add lines, text, etc. on the image object
ImageFont: Set the properties of the font. Since emoji is used, you need to use this module to make some adjustments to the font
Go directly to the code:
#!/usr/bin/env python # coding=utf-8 from PIL import Image, ImageDraw, ImageFont image = Image.new(mode='RGBA', size=(400, 50)) draw_table = ImageDraw.Draw(im=image) draw_table.text(xy=(0, 0), text=u'Look up and smile like a full moon', fill='#008B8B', font=ImageFont.truetype('./SimHei.ttf', 50)) image.show() # Display the image directly image.save('full moon.png', 'PNG') # Save in the current path, the format is PNG image.close()
Run the code and you can see the generated image. Among them, I uploaded the SimHei font file to Baidu Cloud and extracted the code "4uhw", click here to download. If you mind that the text on the generated transparent background image has a dark outline, you can read this article to remove it.
method
The code is not very long, the following is a brief explanation of several methods used in the code:
1)PIL.Image.new(mode, size, color=0)
Create an image object of the specified size with the given mode
The mode parameter: defines some attributes about pixels in the image. The more common ones are: black and white image L, true color RGB, true color RGBA with transparency, etc., see here for details
size parameter: specify the length and width of the image in pixels, in tuple form
color parameter: Specifies the background color of the image. When the image mode is RGBA, if this parameter is not specified, the default is a transparent background
2)PIL.ImageDraw.Draw(im, mode=None)
Create an object to paint on the image object
im parameter: the created image object
The mode parameter: defines some attributes about pixels in the image. If not given, it will be consistent with the mode parameter of the passed in image object
3)PIL.ImageDraw.ImageDraw.text(xy, text, fill=None, font=None, direction=None)
Write a line of text at the specified position of the picture, multiline text needs to use the multiline_text method
xy parameter: specify the position of the text from the upper left corner, in tuple form
text parameter: text content
fill parameter: text color, supports English words and hexadecimal notation
font parameter: font object, generally constructed by ImageFont module, used to specify the font file location and font size
direction parameter: text content direction, requires libraqm support
(This method has other parameters, please click here for details )
References:
If there is any inappropriateness in the text, I hope everyone will tolerate and point out, thank you