程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python PIL image operation (II) drawing

編輯:Python

Catalog

ImageDraw modular

Draw a straight line

Draw the arc

Draw the ellipse

Draw chord

Drawing fan

Draw polygon

Draw a rectangle

Draw text

Draw points

ImageFont modular


ImageDraw modular

ImageDraw The module realizes the drawing function . You can draw by creating pictures 2D Images ; You can also draw on the original picture , In order to decorate the picture or annotate the picture . stay ImageDraw When drawing a module, you need to first create a ImageDraw.Draw object , And provide parameters that point to the file . Then reference the created Draw Object method for drawing . Finally, save or directly output the drawn image :drawObject=ImageDraw.Draw(black)

Draw a straight line

drawObject.line ([x,y1,x2,y2], fill= None,width =0,joint= None)

Said to (x1,y1) As the starting point , With (x2,y2) Draw a straight line for the ending point .[x1,y,2,y2] It can also be written as (x1,y1,x2,y2)、[x1,y1)(x2,y2)]; fill Used to set the specified line color ; width Set the width of the line ; joint Represents the union type between a series of lines . It can be “ curve ”.

Draw the arc

drawObject.arc ([x1,y1 ,x2,y2],tart,end,fill=None,width=0)

In the upper left corner, the coordinates are (x1,y1), The coordinates in the lower right corner are (x2,y2) In a rectangular area of ( round O Inside ), With start Is the starting angle , With end For the ending angle , Truncation circle O And draw a part of the arc . If [x1,y1,x2,y2] The area is not square , Then in the largest ellipse in the region, the fragment is intercepted according to the angle . Parameters fill and width And line In the same way .

Draw the ellipse

drawObject.elipse([x1,y1, x2,y2],fill=None,outline=None,width=0)

Use the same arc similar , Used to draw circles ( Or ellipses ).outline Indicates that only the color of the circle is specified .

Draw chord

drawObject.chord([x1,y1, x2,y2],start, end, fill=None,outline=None,width=0)

Use the same arc similar , Used to draw circles from start To end Chord .fill Indicates that the space between the chord and the arc is filled with the specified color , Set to outline Indicates that only the color of chords is specified .

Drawing fan

drawObiect.pieslice ([x1,y1,x2,y2],start,end,fill=None,outline=None,width=0)

Use the same elipse similar , Used to draw the fan-shaped area between the start and end angles .fill Indicates that the sector area is filled with the specified color , Set to outline Indicates that only the specified color is used to trace the outline of the area .

Draw polygon

drawbjct.polygon([x1,y1,x2,y2,...],fill= None,outline=None)

Draw a polygon according to coordinates ,Python According to the first parameter (x,y) Coordinate pairs , Connect the entire graph .fill Indicates that the polygon area is filled with the specified color ,outline It is only used to set the specified color to trace the outline of the area .

Draw a rectangle

drawObject.rectangle([x1,y1,x2,y2], fill=None,outline=None,width=0)

Draw a rectangle in the specified area ,(x1,y1) Represents the coordinates of the upper left corner of the rectangle ,(x2,y2) Represents the coordinates of the lower right corner of the rectangle .fill Used to fill the rectangular area with color ,outline Used to outline the area .

Draw text

drawObject.text(position,text,fill=None,font=None,anchor=None,spacing=0,align=“left",direction =None,features=None,language=None)

Add text to the image . The parameter position It's a binary , Used to specify the coordinates of the upper left corner of the text ; text Indicates the text content to be written ; fill Represents the color of the text ; font It has to be for ImageFont Specified in the font type ;spacing Indicates the number of pixels between rows ;align Indicate location “left",“center" or “right";direction Indicates the direction of the text . It can be ’rtl’( From right to left ),'ltr' ( From left to right ) or ’ttb’ ( From top to bottom ).

Draw points

drawObject.point(x,y,fill=None)

Draw a point at a given coordinate ( Single pixel ).

ImageFont modular

ImageFont The function of is to realize the processing of font and font . Common built-in functions :

①load(): Load a font from the specified file , This function returns the corresponding font object . If the function fails , Then there will be IOError abnormal . Grammar format :ImageFont.load( file name )

②load_path(): And the function load() equally , But if the current path is not specified , From the file sys.path Start finding the specified font file . Grammar format :ImageFont.load_path( file name )

③truetype(): There are two definition formats :

The first 1 The function of this format is to load a TrueType perhaps OpenType The font file , And create a font object . stay Windows In the system , If the specified file does not exist , The loader will look by Windows Does it exist in the font directory of . Grammar format :ImageFont.truetype(file,size)

The first 2 The function of this format is to load a TrueType perhaps OpenType The font file , And create a font object . The usual encoding method is “unic”(Unicode)、“symb” (MicrosofSymbol)、“ADOB”(Adobe Standard)、“ ADBE”(Adobe Expert) and “armn”(Apple Roman). Grammar format :ImageFont.truetype (file,size,encoding=value)

④load_default(): The function is to load a default font . Grammar format :ImageFont.load_default()

⑤getsize(): Returns the width and height of the given text , The return value is a binary . Grammar format :ImageFont.getsize()

from PIL import Image, ImageDraw, ImageFont
img_background = Image.new('RGB', (800, 500), '#B9DEF5')
img = ImageDraw.Draw(img_background)
img.line((0, 0, 200, 200), fill='yellow', width=3)
img.arc((0, 0, 200, 200), 0, 360, 'red', 3)
img.ellipse((200, 100, 300, 300), fill='green', outline='blue', width=3)
img.pieslice((200, 200, 500, 500), 30, 150, outline='pink', fill='yellow', width=2)
img.polygon((700, 500, 450, 400, 600, 650, 700, 700), fill='#96D767', outline='#A6D9AE')
img.text((300, 10), text=' Learn together python', fill='black', font=ImageFont.truetype('C:/Windows/Fonts/STXINGKA.TTF', 50),
spacing=5,align='center')
img_background.show()


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved