获取Draw对象
img = Image.open("文件路径")
pen = ImageDraw.Draw(img)
(1) 绘制直线
def line(self, [x1,y1,x2,y2], fill=None, width=0, joint=None)
(2) 绘制圆弧
def arc(self, [x1,y1,x2,y2], start, end, fill=None, width=1)
(3) 绘制椭圆
def ellipse(self, [x1,y1,x2,y2], fill=None, outline=None, width=1)
(4) 绘制弦
def chord(self, [x1,y1,x2,y2], start, end, fill=None, outline=None, width=1)
(5) 绘制扇形
def pieslice(self, [x1,y1,x2,y2], start, end, fill=None, outline=None, width=1)
(6) 绘制多边形
def polygon(self, [x1,y1,x2,y2], fill=None, outline=None)
(7) 绘制矩形
def rectangle(self, [x1,y1,x2,y2], fill=None, outline=None, width=1)
(8) 绘制文字
def text(
self,
xy,
text,
fill=None,
font=None,
anchor=None,
spacing=4,
align="left",
direction=None,
features=None,
language=None,
):
(9) 绘制点
def point(self, xy, fill=None)
【示例】创建图片的方式来绘制
from PIL import ImageDraw,Image,ImageFont
img = Image.new("RGB", (300, 300))
pen = ImageDraw.Draw(img)
pen.rectangle((10,10,250,250),fill='black',outline='red')
img_font = ImageFont.truetype('SIMLI.TTF',20)
pen.text((20,20),'这是一个测试', fill='white', font=img_font)
img.show()
【示例】在原图上绘制一个椭圆
from PIL import Image,ImageDraw
img = Image.open('picture\cup.jpg')
pen = ImageDraw.Draw(img)
pen.arc((0,0,img.width-1,img.height-1),0,360,fill='blue',width=10)
img.show()
(1)load函数
指定的文件
中加载一种字体,该函数返回对应的字体对象def load(filename)
(2)load_path函数
没有指定
当前路径,就会从文件 sys.path 开 始查找指定的字体文件def load_path(filename)
(3)load_default函数
默认的字体
def load_default()
(4)getsize函数
宽度
和高度
,返回值是一个二元组
def getsize(self, text, direction=None, features=None, language=None, stroke_width=0)
(5)truetype函数
def truetype(font=None, size=10, index=0, encoding="", layout_engine=None)
第 1 种格式的功能是加载一个 TrueType 或者 OpenType 字体文件,并且创建一个字体对象。在 Windows 系统中,如果指定的文件不存在,加载器 就会顺便看看 Windows 的字体目录下它是否存在
Image.truetype(file,size)
第 2 种格式的功能是,加载一个 TrueType 或者 OpenType 字体文件,并且创建一个字体 对象。
编码方式(encoding) | 含义 |
---|---|
unic | Unicode |
symb | MicrosoftSymbol |
ADOB | Adobe Standard |
ADBE | Adobe Expert |
armn | Apple Roman |
Image.truetype(file,size,encoding=value)
【示例】在原图上面绘制文字
from PIL import ImageFont, ImageDraw, Image
img = Image.open('picture\cup.jpg')
pen = ImageDraw.Draw(img)
img_font = ImageFont.load_default()
pen.text((20, 10), 'ceshi', font=img_font, fill='white')
img_font = ImageFont.truetype('SIMYOU.TTF', 50)
pen.text((20, 80), '测试', font=img_font, fill='red')
img_font = ImageFont.truetype('SIMYOU.TTF',50)
pen.text((20, 150), '测试', font=img_font, fill='blue')
img.show()
绘制十字
from PIL import Image, ImageDraw
img = Image.open('picture\cup.jpg')
pen = ImageDraw.Draw(img)
pen.line([0, 0, img.width, img.height], fill='blue', width=5)
pen.line([0, img.height, img.width, 0], fill='red', width=5)
img.show()
生成 随机验证码
from PIL import Image, ImageDraw, ImageFont
import random
img = Image.new("RGB", (100, 100), "white")
pen = ImageDraw.Draw(img)
# 获取一个随机颜色
def get_color():
return (random.randint(200, 255), random.randint(200, 255), random.randint(200, 255))
# 获取一个随机大写字符
def get_char():
return chr(random.randint(65, 90))
# 图片每个点的颜色随机
for x in range(img.width):
for y in range(img.height):
pen.point((x,y), fill=get_color())
# 在图片上生成4个随机字母
img_font = ImageFont.truetype('simsun.ttc', 36)
for i in range(4):
pen.text((10 + i * 20, 50), get_char(), font=img_font, fill="red")
# 干扰线
for i in range(2):
pen.line([10, 10, 80, 80], fill="green", width=4)
img.show()
绘制九宫格
from PIL import Image, ImageDraw
width = 300
height = 300
img = Image.new("RGB", (width, height), "blue")
pen = ImageDraw.Draw(img)
def get_color(x,y):
a = x // 100 + y // 100
if a == 0:
return (255, 0, 0)
if a == 1:
return (0, 255, 0)
if a == 2:
return (0, 0, 255)
if a == 3:
return (0, 0, 0)
if a == 4:
return (255, 255, 255)
for x in range(width):
for y in range(height):
pen.point((x,y), fill=get_color(x,y))
img.show()
将图片中所有黄色像素点修改成红色像素点
from PIL import Image, ImageDraw
img = Image.open("picture\cup.jpg")
pen = ImageDraw.Draw(img)
# 正宗的黄色(255,255,0) 正宗红色(255,0,0)
def modify_color(x,y):
old_color = img.getpixel((x,y))
if old_color[0] > 60 and old_color[1] > 60 and old_color[2] < 60 :
return (255, 0, 0)
else:
return old_color
for x in range(img.width):
for y in range(img.height):
pen.point((x,y), fill=modify_color(x, y))
img.show()