用随机噪声替换图像中的颜色值

问题描述 投票:0回答:1

我发现了一些与我想要做的事情相近的事情:Python: PIL replace a single RGBA color

但是,在我的场景中,我的图像最初为灰度,并在图像中添加了颜色注释(带有颜色注释的X射线)。我想用随机噪声替换任何非灰度的像素。我的主要问题是用噪声而不是单一颜色替换值。

编辑:我想出了随机噪声部分,现在只是想弄清楚如何将颜色像素与最初为灰度的像素分开。

from PIL import Image
import numpy as np

im = Image.open('test.jpg')

data = np.array(im)   # "data" is a height x width x 3 numpy array
red, green, blue = data.T # Temporarily unpack the bands for readability


# Replace white with random noise...
white_areas = (red == 255) & (blue == 255) & (green == 255)
Z = random.random(data[...][white_areas.T].shape)
data[...][white_areas.T] = Z

im2 = Image.fromarray(data)
im2.show()
python numpy python-imaging-library
1个回答
0
投票

你可以试试

col_areas = np.logical_or(np.not_equal(red, blue), np.not_equal(red, green))
© www.soinside.com 2019 - 2024. All rights reserved.