Python,PIL写中文和画框,mask

    pil_img = Image.fromarray(img_arr)
    pil_draw = ImageDraw.Draw(pil_img) # 图片上打印
    font = ImageFont.truetype('SimHei.ttf', 16)
    # cv_colors = [[]]
    for i, (bbox, label) in enumerate(zip(bboxes, labels)):
        bbox_int = bbox.astype(np.int32)
        poly = [[bbox_int[0], bbox_int[1]], [bbox_int[0], bbox_int[3]],
                [bbox_int[2], bbox_int[3]], [bbox_int[2], bbox_int[1]]]
        np_poly = np.array(poly).reshape((4, 2))
        polygons.append(Polygon(np_poly))
        color.append(bbox_color)
        label_text = class_names[
            label] if class_names is not None else f'class {label}'
        if len(bbox) > 4:
            label_text += f'|{bbox[-1]:.02f}'
        # ax.text(
        #     bbox_int[0],
        #     bbox_int[1],
        #     f'{label_text}',
        #     bbox={
        #         'facecolor': 'black',
        #         'alpha': 0.8,
        #         'pad': 0.7,
        #         'edgecolor': 'none'
        #     },
        #     color=text_color,
        #     fontsize=font_size,
        #     verticalalignment='top',
        #     horizontalalignment='left')
        if segms is not None:
            color_mask = mask_colors[labels[i]]
            # print(segms[i])
            mask = segms[i].astype(bool)
            img[mask] = img[mask] * 0.5 + color_mask * 0.5
            
            mask = segms[i].astype(int)
            bool_mask = np.where(mask, 1, 0)
            binary_mask = bool_mask.astype(np.uint8) * 255
            cv_contours, _ = cv2.findContours(binary_mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
            cv_contour = max(cv_contours, key=cv2.contourArea)  # 选择最大的轮廓(假设目标是最大的轮廓)
            cv_rect = cv2.minAreaRect(cv_contour)  # 计算最小外接矩形
            cv_center, (cv_width, cv_height), cv_angle = cv_rect  # 获取矩形的中心点坐标、宽度、高度和旋转角度
            cv_box = cv2.boxPoints(cv_rect)  # 计算矩形的四个顶点
            cv_box = np.int0(cv_box)         
            
            pil_draw.text((cv_box[1][0], cv_box[1][1] - 10), class_names[label], fill=cv_colors[label], font=font)
            pil_draw.line([(cv_box[0][0], cv_box[0][1]), (cv_box[1][0], cv_box[1][1])], fill=cv_colors[label], width = 2) 
            pil_draw.line([(cv_box[1][0], cv_box[1][1]), (cv_box[2][0], cv_box[2][1])], fill=cv_colors[label], width = 2) 
            pil_draw.line([(cv_box[2][0], cv_box[2][1]), (cv_box[3][0], cv_box[3][1])], fill=cv_colors[label], width = 2) 
            pil_draw.line([(cv_box[3][0], cv_box[3][1]), (cv_box[0][0], cv_box[0][1])], fill=cv_colors[label], width = 2) 
            # cv2.drawContours(cv_img, [cv_box], 0, cv_colors[label], 1)
            # print("-----------------------------------------")
    # cv2.imwrite("./test.jpg", cv_img)
    pil_img.save(out_file)

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值