1.更新pip--cmd使用指令:
python -m pip install --upgrade pip
2.替换包来源--Pycharm-当前项目-File-Settings-Project:项目-Project Interpreter-点击加号-Manager Repositories
https://pypi.tuna.tsinghua.edu.cn/simple/
3.退回到Project Interpreter-搜索Pillow和numpy并安装(点击Install Package)
4.新建文件,运行代码
import numpy as np
from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont
def draw_test():
# 生成深蓝色绘图画布
array = np.ndarray((480, 640, 3), np.uint8)
array[:, :, 0] = 0
array[:, :, 1] = 0
array[:, :, 2] = 100
image = Image.fromarray(array)
# 创建绘制对象
draw = ImageDraw.Draw(image)
# 绘制直线
draw.line((20, 20, 150, 150), 'cyan')
# 绘制矩形
draw.rectangle((100, 200, 300, 400), 'black', 'red')
# 绘制弧
draw.arc((100, 200, 300, 400), 0, 180, 'yellow')
draw.arc((100, 200, 300, 400), -90, 0, 'green')
# 绘制弦
draw.chord((350, 50, 500, 200), 0, 120, 'khaki', 'orange')
# 绘制圆饼图
draw.pieslice((350, 50, 500, 200), -150, -30, 'pink', 'crimson')
# 绘制椭圆
draw.ellipse((350, 300, 500, 400), 'yellowgreen', 'wheat')
# 外切矩形为正方形时椭圆即为圆
draw.ellipse((550, 50, 600, 100), 'seagreen', 'skyblue')
# 绘制多边形
draw.polygon((150, 180, 200, 180, 250, 120, 230, 90, 130, 100), 'olive', 'hotpink')
# 绘制文本
font = ImageFont.truetype("consola.ttf", 40, encoding="unic") # 设置字体
draw.text((100, 50), u'Hello World', 'fuchsia', font)
image.show()
return
draw_test()