我有一个只有文本(总是白色)的图像,我想将文本从它复制到另一个图像.

下面是带有徽标和图像的图像.目前,我正在抓取这个文本的屏幕截图,并将其叠加到另一个图像上.然而,正如你所看到的,我得到了与文本一起的黑色矩形,我如何才能摆脱黑色矩形区域,或者只是将文本从黑色框架复制到图像?

enter image description here enter image description here enter image description here

image_2_replace = cv2.imread(mask_image2)
im2r = cv2.cvtColor(image_2_replace, cv2.COLOR_BGR2RGB)

image_2_title_img = cv2.imread(image_2_title)
image_2_titl_img = cv2.cvtColor(image_2_title_img, cv2.COLOR_BGR2RGB)

im2r_title = cv2.resize(image_2_titl_img, (left_image_msg[2], left_image_msg[3]))

# take the coordinate of the location where i need to put the text screenshot and add it over the image.

im2r[153: 153 + 324, 
     580: 580 + 256] = im2r_title

推荐答案

黑色背景也被复制,因为您将整个剪贴粘贴到您的图像中:

im2r[153: 153 + 324, 
     580: 580 + 256] = im2r_title

但由于你的标题是在漂亮的黑色背景上,你只能复制图片中从黑色中突出的部分.裁剪部分的RGB代码与[0 0 0]不同.

实现可能如下所示(我使用了您的屏幕截图和一些不同的裁剪位置):

im2r[153: 153 + 70,
     280: 580 + 200,
     ] = np.where(im2r_title < [100, 100, 100], im2r[153: 153 + 70,
     280: 580 + 200,
     ], im2r_title)

魔术师np.where(im2r_title < [100, 100, 100],...在这里,我告诉numpy只替换im 2 r图片中 colored颜色 小于[np.where(im2r_title < [100, 100, 100],...np.where(im2r_title < [100, 100, 100],...np.where(im2r_title < [100, 100, 100],...]的像素.这意味着黑色/深灰色区域不会粘贴到图像上.(图像下方的完整代码)

enter image description here

import cv2
import numpy as np
image_2_replace = cv2.imread("92K7B.png")
im2r = cv2.cvtColor(image_2_replace, cv2.COLOR_BGR2RGB)

image_2_title_img = cv2.imread("J33EF.png")
image_2_titl_img = cv2.cvtColor(image_2_title_img, cv2.COLOR_BGR2RGB)

im2r_title = image_2_titl_img[250:320, 200:400]
cv2.imshow("image_2_title_img",im2r_title)

# take the coordinate of the location where i need to put the text screenshot and add it over the image.
im2r[153: 153 + 70,
     280: 580 + 200,
     ] = np.where(im2r_title < [100, 100, 100], im2r[153: 153 + 70,
     280: 580 + 200,
     ], im2r_title)

cv2.imshow("image_2_title_img",im2r)
cv2.waitKey(0)

Python相关问答推荐

带有pandas的分区列上的过滤器的多个条件read_parquet

当测试字符串100%包含查询字符串时,为什么t fuzzywuzzy s Process.extractBests不给出100%分数?

如何匹配3D圆柱体的轴和半径?

使用FASTCGI在IIS上运行Django频道

滚动和,句号来自Pandas列

处理(潜在)不断增长的任务队列的并行/并行方法

运行终端命令时出现问题:pip start anonymous"

Python库:可选地支持numpy类型,而不依赖于numpy

driver. find_element无法通过class_name找到元素'""

Scrapy和Great Expectations(great_expectations)—不合作

当我try 在django中更新模型时,模型表单数据不可见

将pandas导出到CSV数据,但在此之前,将日期按最小到最大排序

Pandas Data Wrangling/Dataframe Assignment

无论输入分辨率如何,稳定扩散管道始终输出512 * 512张图像

* 动态地 * 修饰Python中的递归函数

通过追加列表以极向聚合

Polars map_使用多处理对UDF进行批处理

在Python中控制列表中的数据步长

如何获得3D点的平移和旋转,给定的点已经旋转?

一个telegram 机器人应该发送一个测验如何做?""