Movatterモバイル変換


[0]ホーム

URL:


ContentsMenuExpandLight modeDark modeAuto light/dark, in light modeAuto light/dark, in dark modeSkip to content
Pillow (PIL Fork) 12.0.0 documentation
Light LogoDark Logo
Pillow (PIL Fork) 12.0.0 documentation
Back to top

Source code for PIL.ImageDraw2

## The Python Imaging Library# $Id$## WCK-style drawing interface operations## History:# 2003-12-07 fl   created# 2005-05-15 fl   updated; added to PIL as ImageDraw2# 2005-05-15 fl   added text support# 2005-05-20 fl   added arc/chord/pieslice support## Copyright (c) 2003-2005 by Secret Labs AB# Copyright (c) 2003-2005 by Fredrik Lundh## See the README file for information on usage and redistribution.#"""(Experimental) WCK-style drawing interface operations.. seealso:: :py:mod:`PIL.ImageDraw`"""from__future__importannotationsfromtypingimportAny,AnyStr,BinaryIOfrom.importImage,ImageColor,ImageDraw,ImageFont,ImagePathfrom._typingimportCoords,StrOrBytesPath
[docs]classPen:"""Stores an outline color and width."""def__init__(self,color:str,width:int=1,opacity:int=255)->None:self.color=ImageColor.getrgb(color)self.width=width
[docs]classBrush:"""Stores a fill color"""def__init__(self,color:str,opacity:int=255)->None:self.color=ImageColor.getrgb(color)
[docs]classFont:"""Stores a TrueType font and color"""def__init__(self,color:str,file:StrOrBytesPath|BinaryIO,size:float=12)->None:# FIXME: add support for bitmap fontsself.color=ImageColor.getrgb(color)self.font=ImageFont.truetype(file,size)
[docs]classDraw:""" (Experimental) WCK-style drawing interface """def__init__(self,image:Image.Image|str,size:tuple[int,int]|list[int]|None=None,color:float|tuple[float,...]|str|None=None,)->None:ifisinstance(image,str):ifsizeisNone:msg="If image argument is mode string, size must be a list or tuple"raiseValueError(msg)image=Image.new(image,size,color)self.draw=ImageDraw.Draw(image)self.image=imageself.transform:tuple[float,float,float,float,float,float]|None=None
[docs]defflush(self)->Image.Image:returnself.image
[docs]defrender(self,op:str,xy:Coords,pen:Pen|Brush|None,brush:Brush|Pen|None=None,**kwargs:Any,)->None:# handle color argumentsoutline=fill=Nonewidth=1ifisinstance(pen,Pen):outline=pen.colorwidth=pen.widthelifisinstance(brush,Pen):outline=brush.colorwidth=brush.widthifisinstance(brush,Brush):fill=brush.colorelifisinstance(pen,Brush):fill=pen.color# handle transformationifself.transform:path=ImagePath.Path(xy)path.transform(self.transform)xy=path# render the itemifopin("arc","line"):kwargs.setdefault("fill",outline)else:kwargs.setdefault("fill",fill)kwargs.setdefault("outline",outline)ifop=="line":kwargs.setdefault("width",width)getattr(self.draw,op)(xy,**kwargs)
[docs]defsettransform(self,offset:tuple[float,float])->None:"""Sets a transformation offset."""(xoffset,yoffset)=offsetself.transform=(1,0,xoffset,0,1,yoffset)
[docs]defarc(self,xy:Coords,pen:Pen|Brush|None,start:float,end:float,*options:Any,)->None:""" Draws an arc (a portion of a circle outline) between the start and end angles, inside the given bounding box. .. seealso:: :py:meth:`PIL.ImageDraw.ImageDraw.arc` """self.render("arc",xy,pen,*options,start=start,end=end)
[docs]defchord(self,xy:Coords,pen:Pen|Brush|None,start:float,end:float,*options:Any,)->None:""" Same as :py:meth:`~PIL.ImageDraw2.Draw.arc`, but connects the end points with a straight line. .. seealso:: :py:meth:`PIL.ImageDraw.ImageDraw.chord` """self.render("chord",xy,pen,*options,start=start,end=end)
[docs]defellipse(self,xy:Coords,pen:Pen|Brush|None,*options:Any)->None:""" Draws an ellipse inside the given bounding box. .. seealso:: :py:meth:`PIL.ImageDraw.ImageDraw.ellipse` """self.render("ellipse",xy,pen,*options)
[docs]defline(self,xy:Coords,pen:Pen|Brush|None,*options:Any)->None:""" Draws a line between the coordinates in the ``xy`` list. .. seealso:: :py:meth:`PIL.ImageDraw.ImageDraw.line` """self.render("line",xy,pen,*options)
[docs]defpieslice(self,xy:Coords,pen:Pen|Brush|None,start:float,end:float,*options:Any,)->None:""" Same as arc, but also draws straight lines between the end points and the center of the bounding box. .. seealso:: :py:meth:`PIL.ImageDraw.ImageDraw.pieslice` """self.render("pieslice",xy,pen,*options,start=start,end=end)
[docs]defpolygon(self,xy:Coords,pen:Pen|Brush|None,*options:Any)->None:""" 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. .. seealso:: :py:meth:`PIL.ImageDraw.ImageDraw.polygon` """self.render("polygon",xy,pen,*options)
[docs]defrectangle(self,xy:Coords,pen:Pen|Brush|None,*options:Any)->None:""" Draws a rectangle. .. seealso:: :py:meth:`PIL.ImageDraw.ImageDraw.rectangle` """self.render("rectangle",xy,pen,*options)
[docs]deftext(self,xy:tuple[float,float],text:AnyStr,font:Font)->None:""" Draws the string at the given position. .. seealso:: :py:meth:`PIL.ImageDraw.ImageDraw.text` """ifself.transform:path=ImagePath.Path(xy)path.transform(self.transform)xy=pathself.draw.text(xy,text,font=font.font,fill=font.color)
[docs]deftextbbox(self,xy:tuple[float,float],text:AnyStr,font:Font)->tuple[float,float,float,float]:""" Returns bounding box (in pixels) of given text. :return: ``(left, top, right, bottom)`` bounding box .. seealso:: :py:meth:`PIL.ImageDraw.ImageDraw.textbbox` """ifself.transform:path=ImagePath.Path(xy)path.transform(self.transform)xy=pathreturnself.draw.textbbox(xy,text,font=font.font)
[docs]deftextlength(self,text:AnyStr,font:Font)->float:""" Returns length (in pixels) of given text. This is the amount by which following text should be offset. .. seealso:: :py:meth:`PIL.ImageDraw.ImageDraw.textlength` """returnself.draw.textlength(text,font=font.font)
Copyright © 1995-2011 Fredrik Lundh and contributors, 2010 Jeffrey A. Clark and contributors.
Made withSphinx and@pradyunsg'sFuro

[8]
ページ先頭

©2009-2025 Movatter.jp