def plot_line(img):


    cv2.line(img,(120,120),(990,120),(255,0,0),3)
    cv2.line(img,(990,120),(990, 640),(255,0,0),3)


    cv2.line(img, (990,640), (120,640), (255, 0, 0), 3)
    cv2.line(img, (120,640), (120, 120), (255, 0, 0), 3)



    img_PIL = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))

    # 字体  字体*.ttc的存放路径一般是: /usr/share/fonts/opentype/noto/ 查找指令locate *.ttc
    font = ImageFont.truetype('/home/xx/NotoSansCJK-Black.ttc', 40)
    # 字体颜色
    fillColor = (0, 0, 255)
    # 文字输出位置
    position = (250, 40)
    # 输出内容
    str = '显示中文'

    # 需要先把输出的中文字符转换成Unicode编码形式
    # if not isinstance(str, unicode):
    #     str = str.decode('utf8')
    # str = str.encode('utf8')
    draw = ImageDraw.Draw(img_PIL)
    draw.text(position, str, font=font, spacing= 2,fill=fillColor)
    # 使用PIL中的save方法保存图片到本地
    # img_PIL.save('02.jpg', 'jpeg')

    # 转换回OpenCV格式
    img = cv2.cvtColor(numpy.asarray(img_PIL), cv2.COLOR_RGB2BGR)
    return img

Logo

技术共进,成长同行——讯飞AI开发者社区

更多推荐