发布于 2015-09-11 16:10:41 | 412 次阅读 | 评论: 0 | 来源: 网络整理
The ImageDraw module provide simple 2D graphics for Image objects. You can use this module to create new images, annotate or retouch existing images, and to generate graphics on the fly for web use.
For a more advanced drawing library for PIL, see the aggdraw module.
from PIL import Image, ImageDraw
im = Image.open("lena.pgm")
draw = ImageDraw.Draw(im)
draw.line((0, 0) + im.size, fill=128)
draw.line((0, im.size[1], im.size[0], 0), fill=128)
del draw
# write to stdout
im.save(sys.stdout, "PNG")
The graphics interface uses the same coordinate system as PIL itself, with (0, 0) in the upper left corner.
To specify colors, you can use numbers or tuples just as you would use with PIL.Image.Image.new() or PIL.Image.Image.putpixel(). For “1”, “L”, and “I” images, use integers. For “RGB” images, use a 3-tuple containing integer values. For “F” images, use integer or floating point values.
For palette images (mode “P”), use integers as color indexes. In 1.1.4 and later, you can also use RGB 3-tuples or color names (see below). The drawing layer will automatically assign color indexes, as long as you don’t draw with more than 256 colors.
See Color Names for the color names supported by Pillow.
PIL can use bitmap fonts or OpenType/TrueType fonts.
Bitmap fonts are stored in PIL’s own format, where each font typically consists of a two files, one named .pil and the other usually named .pbm. The former contains font metrics, the latter raster data.
To load a bitmap font, use the load functions in the ImageFont module.
To load a OpenType/TrueType font, use the truetype function in the ImageFont module. Note that this function depends on third-party libraries, and may not available in all PIL builds.
Creates an object that can be used to draw in the given image.
Note that the image will be modified in place.
Draws an arc (a portion of a circle outline) between the start and end angles, inside the given bounding box.
参数: |
|
---|
Draws a bitmap (mask) at the given position, using the current fill color for the non-zero portions. The bitmap should be a valid transparency mask (mode “1”) or matte (mode “L” or “RGBA”).
This is equivalent to doing image.paste(xy, color, bitmap).
To paste pixel data into an image, use the paste() method on the image itself.
Same as arc(), but connects the end points with a straight line.
参数: |
|
---|
Draws an ellipse inside the given bounding box.
参数: |
|
---|
Draws a line between the coordinates in the xy list.
参数: |
|
---|
Same as arc, but also draws straight lines between the end points and the center of the bounding box.
参数: |
|
---|
Draws points (individual pixels) at the given coordinates.
参数: |
|
---|
Draws a polygon.
The polygon outline consists of straight lines between the given coordinates, plus a straight line between the last and the first coordinate.
参数: |
|
---|
Draws a rectangle.
参数: |
|
---|
警告
This method is experimental.
Draw a shape.
Draws the string at the given position.
参数: |
|
---|
Return the size of the given string, in pixels.
参数: |
|
---|
The Draw class contains a constructor and a number of methods which are provided for backwards compatibility only. For this to work properly, you should either use options on the drawing primitives, or these methods. Do not mix the old and new calling conventions.
1.1.5 版后已移除.
Sets the color to use for subsequent draw and fill operations.
1.1.5 版后已移除.
Sets the fill mode.
If the mode is 0, subsequently drawn shapes (like polygons and rectangles) are outlined. If the mode is 1, they are filled.
1.1.5 版后已移除.
Sets the default font to use for the text method.
参数: | font – An ImageFont instance. |
---|