简体   繁体   English

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

[英]How to change the color for a certain area of an image?

I'm trying to cover the images inside of a paragraph in python.我试图覆盖 python 中段落内的图像。

Here is the original picture and there are two images in the middle of the first paragraph.这是原始图片,第一段中间有两张图片。

在此处输入图像描述

Sorry for the big image file.. I want to convert the two images in the middle of the first paragraph into plain white color(to cover them with plain colors).对不起大图像文件..我想将第一段中间的两个图像转换为纯白色(用纯色覆盖它们)。 I have the coordinates for these two images, but how can I just change the color in these particular areas?我有这两张图片的坐标,但是我怎样才能改变这些特定区域的颜色呢?

Here is the x,y coordinates for these two images:这是这两个图像的 x,y 坐标:

image_1:图像_1:

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

image_2:图像_2:

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

Please help!请帮忙! Thank you very much!!非常感谢!!

Here's how to "redact" portions of the image given a top-left and bottom-right corner.以下是如何在给定左上角和右下角的情况下“编辑”图像的部分。

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

I don't think the boxes you gave are correct.我不认为你给的盒子是正确的。 The second one is huge and the first is tiny.第二个很大,第一个很小。 Here's a picture using those boxes:这是使用这些盒子的图片:

在此处输入图像描述

This code should work for any boxes though, so just adjust the corner coordinates to the right spot and it'll work.不过,这段代码应该适用于任何盒子,所以只需将角坐标调整到正确的位置,它就会起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM