社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Python

如何在Python中将文本作为水印垂直打印到图像上

Muhammad Awais • 3 年前 • 1449 次点击  

我这是我的密码

# 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社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/129415
 
1449 次点击  
文章 [ 2 ]  |  最新文章 3 年前
marcos
Reply   •   1 楼
marcos    3 年前

将图像顺时针旋转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)
Souvik Ghosh
Reply   •   2 楼
Souvik Ghosh    3 年前

试试这个

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))