如何更改图像特定区域的颜色?

2024-05-18 23:07:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图用python来覆盖段落中的图像

这是原始图片,第一段中间有两个图像。

enter image description here

很抱歉,这个大图像文件。。我想把第一段中间的两幅图像转换成纯白色(用平淡的颜色覆盖它们)。我有这两幅图像的坐标,但是我如何才能改变这些特定区域的颜色呢

这是这两幅图像的x,y坐标:

图1:

left, right = 678, 925
top, bottum = 325, 373

图2:

left, right = 130, 1534
top, bottum = 403, 1508

请帮忙!非常感谢


Tags: 图像right区域颜色top图像文件图片left
1条回答
网友
1楼 · 发布于 2024-05-18 23:07:38

下面是如何“编辑”给定左上角和右下角的图像部分

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

我认为你给的盒子不对。第二个很大,第一个很小。下面是使用这些框的图片:

enter image description here

这段代码应该适用于任何框,所以只要将角坐标调整到正确的位置,它就会工作

相关问题 更多 >

    热门问题