python - 如何更改图像特定区域的颜色?
问题描述
我试图在 python 中覆盖段落内的图像。
这是原始图片,第一段中间有两张图片。
对不起大图像文件..我想将第一段中间的两个图像转换为纯白色(用纯色覆盖它们)。我有这两张图片的坐标,但是我怎样才能改变这些特定区域的颜色呢?
这是这两个图像的 x,y 坐标:
图像_1:
left, right = 678, 925
top, bottum = 325, 373
图像_2:
left, right = 130, 1534
top, bottum = 403, 1508
请帮忙!非常感谢你!!
解决方案
以下是如何在给定左上角和右下角的情况下“编辑”图像的部分。
import cv2
import numpy as np
# load image
img = cv2.imread("page.jpg");
# target boxes
boxes = [];
# first box
tl = [678, 325];
br = [925, 373];
boxes.append([tl, br]);
# second box
tl = [130, 403];
br = [1534, 1508];
boxes.append([tl, br]);
# redact with numpy slicing
for box in boxes:
tl, br = box;
img[tl[1]:br[1], tl[0]:br[0]] = [255, 255, 255]; # replace with white
# show image
cv2.imshow("Redacted", img);
cv2.waitKey(0);
cv2.imwrite("redacted.png", img); # save
我不认为你给的盒子是正确的。第二个很大,第一个很小。这是使用这些盒子的图片:
不过,这段代码应该适用于任何盒子,所以只需将角坐标调整到正确的位置,它就会起作用。
推荐阅读
- reactjs - 如何将从 Firestore 获取的数据作为道具传递给组件
- javascript - I want to hide the parent div if child div is empty as Filter results empty
- reactjs - After infinite loading using useSWRInfinite, the css is not working for some elements
- c# - How can I have multiple database instances for component tests
- sql - 我想使用字符串中的正则表达式函数在第二个点(。)之前提取文本,例如:
- spring-boot - How to register and retrieve roles with privileges in spring boot?
- javascript - React 组件渲染但未安装,因此无法设置状态
- python - 在 scipy kdtree 中插入对象?
- symfony - 检索用户的收藏夹 symfony
- excel - excel日期自动重排问题