Python Imaging Library:ImageDraw Module(图像绘制模块)

来源:互联网 发布:星际战甲腐蚀投射数据 编辑:程序博客网 时间:2024/06/01 18:53
Python Imaging Library:ImageDraw Module(图像绘制模块)

# 图像绘制库
Mode_NewImg = "RGB"
Width_NewImg = 1000
Height_NewImg = 1000
Color_NewImg = "#78b8e7"
PilImg_Draw = Image.new(Mode_NewImg, (Width_NewImg, Height_NewImg),
                        Color_NewImg)


# 绘制图像
Draw = ImageDraw.Draw(PilImg_Draw)


# 绘制圆弧(arc)
# draw.arc(xy, start, end, options)
# draw.arc([x1, y1, x2, y2], startAngle, endAngle, options)
# 在左上角坐标为(x1,y1),右下角坐标为 (x2,y2)的矩形区域内,
# 以starangle为起始角度,endAngle为终止角度,
# 截取圆O的一部分圆弧绘制出来。如果[x1,y1,x2,y2]区域不是正方形,
# 则在该区域内的最大椭圆中根据角度截取片段。注意:
# [x1,y1,x2,y2]规定矩形框的水平中位线为0度角,
# 角度顺时针变大(与数学坐标系规定方向相反),
# 绘制一个90度蓝色圆弧
Draw.arc((0, 0) + (PilImg_Draw.width/2, PilImg_Draw.height/2),
         30, 150, fill="blue")
# 绘制一个红色上半圆弧
Draw.arc((0, 0) + (PilImg_Draw.width/2, PilImg_Draw.height/2),
         180, 360, fill="red")
# 绘制一个右半椭圆,只需改区域大小为长方形
Draw.arc((0, 0) + (PilImg_Draw.width/2, PilImg_Draw.height/2-150),
         90, 270, fill="green")


# 绘制正弦(chord)
# Draw.chord(xy, start, end, options)
# 用法与arc相同,用来画圆从startAngle到endAngle的弦。
# 绘制一条弦
Draw.chord((PilImg_Draw.width/2, 0) +
           (PilImg_Draw.width, PilImg_Draw.height/2),
           270, 360, outline="red")
# 绘制弦并且将弦与弧包围区域涂色
Draw.chord((PilImg_Draw.width/2, 0) +
           (PilImg_Draw.width, PilImg_Draw.height/2),
           90, 180, fill="red")


# 绘制椭圆(ellipse)
# draw.ellipse(xy, options)
Draw.ellipse((PilImg_Draw.width/2, 100) +
             (PilImg_Draw.width, PilImg_Draw.height/2-100),
             outline=128)
Draw.ellipse((PilImg_Draw.width/2, 100) +
             (PilImg_Draw.width, PilImg_Draw.height/2-100),
             fill="blue")


# 绘制直线(line)
# draw.line(xy, options)
Draw.line((0, 0) + PilImg_Draw.size, fill=128)
Draw.line((0, PilImg_Draw.size[1], PilImg_Draw.size[0], 0), fill=128)


# 绘制起始角度间的扇形区域(pieslice)
# draw.pieslice(xy, start, end, options)
# 绘制180度到210度的扇形区域轮廓
Draw.pieslice((0, PilImg_Draw.height/2) +
              (PilImg_Draw.width/2, PilImg_Draw.height),
              180, 210, outline=128)
# 绘制60度到90度的扇形区域
Draw.pieslice((0, PilImg_Draw.height/2) +
              (PilImg_Draw.width/2, PilImg_Draw.height),
              60, 90, fill="blue")


# 绘制点(point)
# draw.point(xy, options)
Draw.point((PilImg_Draw.width/2, PilImg_Draw.height/2+10),
           fill="black")


# 绘制多边形(polygon)
# draw.polygon(xy, options)
Draw.polygon([(200, 200), (600, 300), (300, 600)],
             outline="green")
Draw.polygon([(300, 300), (500, 300), (300, 500), (500, 500)],
             fill="#34a2b6")
             
# 绘制矩形(rectangle)
# draw.rectangle(box, options)
Draw.rectangle((750, 750, 850, 850), outline="red")
Draw.rectangle((850, 850, 900, 920), fill="#9934c5")
               
# 绘制文本(text)
# draw.text(position, string, options)
Text = "I love python!"
Draw.text([600, 600], Text, "red")


# 获取文本位置
# width, height = Draw.textsize(Text, "red")
# print("文本位置:", width, height)
PilImg_Draw.show()

阅读全文
0 0
原创粉丝点击