image.py 文件源码

python
阅读 33 收藏 0 点赞 0 评论 0

项目:ml-utils 作者: LinxiFan 项目源码 文件源码
def draw_text(img, text, box, color='bw'):
    """
    FONT_HERSHEY_COMPLEX
    FONT_HERSHEY_COMPLEX_SMALL
    FONT_HERSHEY_DUPLEX
    FONT_HERSHEY_PLAIN
    FONT_HERSHEY_SCRIPT_COMPLEX
    FONT_HERSHEY_SCRIPT_SIMPLEX
    FONT_HERSHEY_SIMPLEX
    FONT_HERSHEY_TRIPLEX
    FONT_ITALIC
    """
    x, y, w, h = box
    font = cv2.FONT_HERSHEY_DUPLEX
    region = crop(img, box)
    if color == 'bw':
        brightness = np.mean(cv2.cvtColor(region, cv2.COLOR_BGR2GRAY))
        if brightness > 127:
            font_color = (0,0,0)
        else:
            font_color = (255,255,255)
    elif color == 'color':
        mean_bg = np.round(np.mean(region, axis=(0, 1)))
        font_color = tuple(map(int, np.array((255,255,255)) - mean_bg))
    else:
        font_color = (255, 0, 0) # blue

    cv2.putText(img, text, (x, y+h), font, 1, font_color, 2)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号