欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

为文字图片添加文本框和文字标注

程序员文章站 2022-06-19 23:38:21
...
# -*- coding: utf-8 -*-#

# -------------------------------------------------------------------------------
# Name:         pictureframe.py
# Description:
# Author:       chyao
# Time:         2020/11/24 18:56
# -------------------------------------------------------------------------------
import cv2
from PIL import Image
from PIL import ImageDraw,ImageFont
import numpy as np




dic = {
    "items": [
        {
            "itemcoord": {
                "x": 89,
                "y": 24,
                "width": 471,
                "height": 41
            },
            "itemconf": 0.979169487953186,
            "itemstring": "在对比之中才能真区领悟故乡这两个字",
            "coords": [],
            "candword": [],
            "parag": {
                "word_size": 37,
                "parag_no": 0
            },
            "coordpoint": {
                "x": [
                    89,
                    24,
                    559,
                    24,
                    559,
                    64,
                    89,
                    64
                ]
            },
            "wordcoordpoint": []
        },
        {
            "itemcoord": {
                "x": 37,
                "y": 83,
                "width": 522,
                "height": 40
            },
            "itemconf": 0.9958563446998596,
            "itemstring": "的意义。在离开的那一刻,才忽然变得无比",
            "coords": [],
            "candword": [],
            "parag": {
                "word_size": 37,
                "parag_no": 0
            },
            "coordpoint": {
                "x": [
                    37,
                    83,
                    558,
                    83,
                    558,
                    122,
                    37,
                    122
                ]
            },
            "wordcoordpoint": []
        },
        {
            "itemcoord": {
                "x": 38,
                "y": 145,
                "width": 526,
                "height": 42
            },
            "itemconf": 0.9541999697685242,
            "itemstring": "深刻和重要。那是一片你只要立于其上,就",
            "coords": [],
            "candword": [],
            "parag": {
                "word_size": 37,
                "parag_no": 0
            },
            "coordpoint": {
                "x": [
                    38,
                    145,
                    563,
                    145,
                    563,
                    186,
                    38,
                    186
                ]
            },
            "wordcoordpoint": []
        },
        {
            "itemcoord": {
                "x": 31,
                "y": 205,
                "width": 540,
                "height": 43
            },
            "itemconf": 0.9737124443054199,
            "itemstring": "能感受到深的归属感与安全感的土地,",
            "coords": [],
           
            "candword": [],
            "parag": {
                "word_size": 37,
                "parag_no": 0
            },
            "coordpoint": {
                "x": [
                    31,
                    205,
                    570,
                    205,
                    570,
                    247,
                    31,
                    247
                ]
            },
            "wordcoordpoint": []
        },
        {
            "itemcoord": {
                "x": 42,
                "y": 267,
                "width": 527,
                "height": 46
            },
            "itemconf": 0.9889481067657471,
            "itemstring": "那是你来时的地方。世界上很难再有这样",
            "coords": [],
            "candword": [],
            "parag": {
                "word_size": 37,
                "parag_no": 0
            },
            "coordpoint": {
                "x": [
                    42,
                    267,
                    568,
                    267,
                    568,
                    312,
                    42,
                    312
                ]
            },
            "wordcoordpoint": []
        },
        {
            "itemcoord": {
                "x": 36,
                "y": 323,
                "width": 527,
                "height": 42
            },
            "itemconf": 0.9322624206542969,
            "itemstring": "一座小城 ,它的每一条街道你都留下过足",
            "coords": [],
            "candword": [],
            "parag": {
                "word_size": 37,
                "parag_no": 0
            },
            "coordpoint": {
                "x": [
                    36,
                    323,
                    562,
                    323,
                    562,
                    364,
                    36,
                    364
                ]
            },
            "wordcoordpoint": []
        },
        {
            "itemcoord": {
                "x": 38,
                "y": 385,
                "width": 510,
                "height": 44
            },
            "itemconf": 0.9571229219436646,
            "itemstring": "迹,每一家店铺你都能叫出名字,每一个",
            "coords": [],
           
            "candword": [],
            "parag": {
                "word_size": 37,
                "parag_no": 0
            },
            "coordpoint": {
                "x": [
                    38,
                    385,
                    547,
                    385,
                    547,
                    428,
                    38,
                    428
                ]
            },
            "wordcoordpoint": []
        },
        {
            "itemcoord": {
                "x": 34,
                "y": 451,
                "width": 521,
                "height": 46
            },
            "itemconf": 0.9586889147758484,
            "itemstring": "季节你都亲身经历过,就算再繁华的",
            "coords": [],
            "candword": [],
            "parag": {
                "word_size": 37,
                "parag_no": 0
            },
            "coordpoint": {
                "x": [
                    34,
                    451,
                    554,
                    451,
                    554,
                    496,
                    34,
                    496
                ]
            },
            "wordcoordpoint": []
        },
        {
            "itemcoord": {
                "x": 34,
                "y": 515,
                "width": 491,
                "height": 52
            },
            "itemconf": 0.920544445514679,
            "itemstring": "都市,在它面前,在你眼中,也一样会",
            "coords": [],
            "candword": [],
            "parag": {
                "word_size": 37,
                "parag_no": 0
            },
            "coordpoint": {
                "x": [
                    34,
                    515,
                    524,
                    515,
                    524,
                    566,
                    34,
                    566
                ]
            },
            "wordcoordpoint": []
        },
        {
            "itemcoord": {
                "x": 39,
                "y": 579,
                "width": 501,
                "height": 43
            },
            "itemconf": 0.929545521736145,
            "itemstring": "然失色。对故乡的思念,是再快的",
            "coords": [],
            "candword": [],
            "parag": {
                "word_size": 37,
                "parag_no": 0
            },
            "coordpoint": {
                "x": [
                    39,
                    579,
                    539,
                    579,
                    539,
                    621,
                    39,
                    621
                ]
            },
            "wordcoordpoint": []
        },
        {
            "itemcoord": {
                "x": 34,
                "y": 637,
                "width": 495,
                "height": 44
            },
            "itemconf": 0.9516671895980835,
            "itemstring": "交通,再便捷的通讯,也无法弥补的",
            "coords": [],
            "candword": [],
            "parag": {
                "word_size": 37,
                "parag_no": 0
            },
            "coordpoint": {
                "x": [
                    34,
                    637,
                    528,
                    637,
                    528,
                    680,
                    34,
                    680
                ]
            },
            "wordcoordpoint": []
        },
        {
            "itemcoord": {
                "x": 408,
                "y": 750,
                "width": 184,
                "height": 50
            },
            "itemconf": 0.6143757104873657,
            "itemstring": "里合喝法",
            "coords": [],
            "candword": [],
            "parag": {
                "word_size": 42,
                "parag_no": 1
            },
            "coordpoint": {
                "x": [
                    408,
                    750,
                    591,
                    750,
                    591,
                    799,
                    408,
                    799
                ]
            },
            "wordcoordpoint": []
        }
    ],
}

def cv2ImgAddText(img, text, left, top, textColor=(0, 255, 0), textSize=20):
    if (isinstance(img, np.ndarray)):  # 判断是否OpenCV图片类型
        img = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
        print(img)
    # 创建一个可以在给定图像上绘图的对象
    draw = ImageDraw.Draw(img)
    # 字体的格式
    fontStyle = ImageFont.truetype(
        "simsun.ttc", textSize, encoding="utf-8")
    # 绘制文本
    draw.text((left, top), text, textColor, font=fontStyle)
    # 转换回OpenCV格式
    return cv2.cvtColor(np.asarray(img), cv2.COLOR_RGB2BGR)

img = cv2.imread("./pics/c.jpg")

for item in dic["items"]:
    dot = item["itemcoord"]
    v1 = (dot["x"], dot["y"])
    v2 = (dot["x"]+dot["width"], dot["y"]+dot["height"])
    cv2.rectangle(img,v1,v2,(0,255,0),4)
    text = item["itemstring"]

    # 中文标注方式
    img = cv2ImgAddText(img,text,dot["x"],dot["y"]-20,(255, 0, 0),27)

    # 非中文标注方式
    # cv2.imwrite(str(i)+".jpg",img)
    # cv2.putText(img, u'中文字串'.encode('utf-8'), v1, font, 2, (0, 0, 255), 1)

cv2.imwrite("ddd.jpg",img)

为文字图片添加文本框和文字标注

相关标签: code