我想要实现类似于以下内容:
我目前在红色背景上显示图像,但不确定如何在上面的图像上绘制半透明矩形以放置文本,以使其弹出更多。我很确定可以使用OpenCV来实现,但是我对Python还是陌生的,这似乎很令人困惑。 (我似乎做得不好,开始惹恼我)。这是我当前的图像(忽略白色轮廓):
最佳答案
这是在Python / OpenCV中获得相同结果的一种方法。
输入:
import cv2
import numpy as np
# load image
img = cv2.imread("chimichanga.jpg")
# define undercolor region in the input image
x,y,w,h = 66,688,998,382
# define text coordinates in the input image
xx,yy = 250,800
# compute text coordinates in undercolor region
xu = xx - x
yu = yy - y
# crop undercolor region of input
sub = img[y:y+h, x:x+w]
# create black image same size
black = np.zeros_like(sub)
# blend the two
blend = cv2.addWeighted(sub, 0.75, black, 0.25, 0)
# draw text on blended image
text = cv2.putText(blend, "CHIMICHANGA", (xu,yu), cv2.FONT_HERSHEY_SIMPLEX, 2, (255,255,255), cv2.LINE_8, bottomLeftOrigin=False )
# copy text filled region onto input
result = img.copy()
result[y:y+h, x:x+w] = text
# write result to disk
cv2.imwrite("chimichanga_result.jpg", result)
# display results
cv2.imshow("BLEND", blend)
cv2.imshow("TEXT", text)
cv2.imshow("RESULT", result)
cv2.waitKey(0)
cv2.destroyAllWindows()
结果:
关于python - 如何在Python中绘制半透明矩形?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61150743/