【问题标题】:How to print text as watermark on images vertically in Python如何在Python中垂直将文本打印为图像上的水印
【发布时间】:2021-12-27 14:00:37
【问题描述】:

这是我的代码

# Json file in which Easyocr anotations have saved.
img = cv2.imread('dummy.jpg')
img1 = img.copy()
#rotoated because anotation have according to vertical alignment of image i have matched the orientation
img1=cv2.rotate(img1,rotateCode=cv2.ROTATE_90_CLOCKWISE)
rects = []
with open('dummy.json') as jsn:
    jsn_dict = json.load(jsn)
for k in jsn_dict['textAnnotations']:
    vertices= k['boundingPoly']['vertices']
    cv2.rectangle(img1,list(vertices[2].values()),list(vertices[0].values()),[0,255,0],10)
# I want to put predicted text on top of bounding boxes vertically because my image is rotated anti clockwise
    cv2.putText(img1, k['description'], list(vertices[0].values()),cv2.FONT_HERSHEY_SIMPLEX,5,[0,255,0],5)

我在上面提到的代码中标记了识别的文本, 第一步是,我将图像放入 OCR 模型中,它会根据图像返回一些值,其中每个检测到的文本都有三个值。这些值是边界框的顶点、已识别的文本和准确率百分比。但我的问题是我的图像被 Exif 方向值旋转但 cv2 将其读取为零角度并且我的文本水平打印。我想在图像上垂直打印文本。我已经尝试了很多次,但无法解决我的问题。我希望我已经解释清楚了

【问题讨论】:

    标签: python numpy opencv-python


    【解决方案1】:

    试试这个

    import cv2
    
    
    def transparentOverlay(src, overlay, pos=(0, 0), scale=1):
    """
    :param src: Input Color Background Image
    :param overlay: transparent Image (BGRA)
    :param pos:  position where the image to be blit.
    :param scale : scale factor of transparent image.
    :return: Resultant Image
    """
    overlay = cv2.resize(overlay, (0, 0), fx=scale, fy=scale)
    h, w, _ = overlay.shape  # Size of foreground
    rows, cols, _ = src.shape  # Size of background Image
    y, x = pos[0], pos[1]  # Position of foreground/overlay image
    
    # loop over all pixels and apply the blending equation
    for i in range(h):
        for j in range(w):
            if x + i >= rows or y + j >= cols:
                continue
            alpha = float(overlay[i][j][3] / 255.0)  # read the alpha channel
            src[x + i][y + j] = alpha * overlay[i][j][:3] + (1 - alpha) * src[x + i][y + j]
    return src
    
    
    def addImageWatermark(LogoImage,MainImage,opacity,pos=(10,100),):
    opacity = opacity / 100
    
    OriImg = cv2.imread(MainImage, -1)
    waterImg = cv2.imread(LogoImage, -1)
    
    tempImg = OriImg.copy()
    print(tempImg.shape)
    
    overlay = transparentOverlay(tempImg, waterImg, pos)
    output = OriImg.copy()
    # apply the overlay
    cv2.addWeighted(overlay, opacity, output, 1 - opacity, 0, output)
    
    cv2.imshow('Life2Coding', output)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
    
    
    if __name__ == '__main__':
    addImageWatermark('./logo.png','./hanif.jpg',100,(10,100))
    

    【讨论】:

    • 兄弟你的解决方案很好,但想打印文本而不是图像
    【解决方案2】:

    将图像顺时针旋转 90º,添加文本,然后将图像旋转回原来的样子。

    # Rotate 90º clockwise
    img_rot = cv2.rotate(img1 , cv2.ROTATE_90_CLOCKWISE)
    # Add your text here, adjusting x and y coordinates to the new orientation.
    #   The new adjusted coordinates will be:
    #   (x2, y2) = (original_height - y, x)
    # [...]
    # Rotate back
    img1 = cv2.rotate(img_rot, cv2.ROTATE_90_CLOCKWISE)
    

    【讨论】:

    • 我使用给定坐标添加文本,当我旋转图像时坐标会改变
    • 是的。这就是为什么原始代码说adjusting x and y coordinates to the new orientation。无论如何,我编辑了坐标应如何更改为顺时针旋转 90º 的答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-02-23
    • 1970-01-01
    • 1970-01-01
    • 2013-12-23
    • 1970-01-01
    • 1970-01-01
    • 2011-03-11
    相关资源
    最近更新 更多