如何更改图像中给定索引的RGB值?这给了我一张空白图片:

import numpy as np
from PIL import Image

target_image = Image.open("img.png")
target_image_array = np.array(target_image)
pixels = target_image_array.reshape(-1, 3)

skip_indices = np.arange(101_600, 254_000)

indices_to_grey_out = np.delete(pixels, skip_indices, 0)

pixels_greyed_out = pixels.copy()
pixels_greyed_out[indices_to_grey_out, [0, 1, 2]] = 0

pixels_greyed_out = pixels_greyed_out.reshape(target_image_array.shape)
modified_img = Image.fromarray(pixels_greyed_out)
modified_img.show()

推荐答案

我根本不明白你在试图对扁平和不连续的索引做什么,但纯粹从你的 comments 来看,我理解你想要影响图像中的一些特定 colored颜色 ,所以我将try 并演示这一点.

这是一个中心为rgb(20, 50, 140),边缘为rgb(40, 70, 160)的径向渐变图像,所以我碰巧知道有4,884个像素(如果这很重要的话,是非直线相邻的),值为rgb(25,55,145)

enter image description here

现在,让我们打开它并将其变成一个Numy数组:

from PIL import Image
import numpy as np

# Open image and make into Numpy array
im = Image.open('a.png').convert('RGB')
na = np.array(im)

# Make Boolean (True/False) mask of interesting pixels
mask = np.all(na==[25,55,145], axis=-1)

# Briefly save the mask so we can see what it selects
Image.fromarray(mask).save('DEBUG-mask.png')

# Let's also count the number of pixels selected by the mask
print(np.count_nonzero(mask))       # prints 4,884

enter image description here

现在,我们可以使用蒙版来更改选定的像素.让我们将原始图像中的遮罩像素设置为红色:

na[mask] = [255,0,0]
Image.fromarray(na).save('DEBUG-1.png')

enter image description here

现在使用蒙版的inverse来影响unselected个像素.让我们把它们变成灰色:

na[~mask] = [128,128,128]
Image.fromarray(na).save('DEBUG-2.png')

enter image description here


如果您想遮罩多个 colored颜色 ,使用可以使用逻辑运算将遮罩组合在一起.所以,假设你想额外屏蔽 colored颜色 rgb(28,58148),你可以在上面的代码末尾添加以下内容:

# Mask for second colour
mask2 = np.all(na==[28,58,148], axis=-1)

# Combine the two masks into new, bigger mask
both = mask | mask2

# Make masked pixels yellow and display
na[both] = [255,255,0]
Image.fromarray(na).show()

enter image description here


如果你想要遮罩range%的 colored颜色 ,我建议你用OpenCVinRange()来设置R、G和B的下限和上限,然后 Select 介于两者之间的所有 colored颜色 .

import cv2

# Mask range 25>R>35, 55>G>65, 145<B<160
colRange = cv2.inRange(na,np.array([25,55,145]) ,np.array([35,65,160]))


# Make range of colours lime green
na[colRange>0] = [0,255,0]

enter image description here


如果有人对我一开始是如何制作径向渐变感兴趣的,我在终端中使用了ImageMagick,并使用了以下命令:

magick -size 500x500 radial-gradient:"rgb(20, 50, 140)-rgb(40, 70, 160)" -depth 8 a.png

一个更简单、更清晰的版本可能是:

magick -size 500x500 radial-gradient:lime-magenta -depth 8 a.png

Python相关问答推荐

2维数组9x9,不使用numpy.数组(MutableSequence的子类)

使用新的类型语法正确注释ParamSecdecorator (3.12)

Pandas 滚动最接近的价值

带条件计算最小值

在Mac上安装ipython

"使用odbc_connect(raw)连接字符串登录失败;可用于pyodbc"

Pandas:将多级列名改为一级

如何使用Python以编程方式判断和检索Angular网站的动态内容?

如何更新pandas DataFrame上列标题的de值?

让函数调用方程

如何获取Python synsets列表的第一个内容?

如何在GEKKO中使用复共轭物

在Django中重命名我的表后,旧表中的项目不会被移动或删除

简单 torch 模型测试:ModuleNotFoundError:没有名为';Ultralytics.yolo';

裁剪数字.nd数组引发-ValueError:无法将空图像写入JPEG

PYTHON中的selenium不会打开 chromium URL

Pandas 删除只有一种类型的值的行,重复或不重复

利用广播使减法更有效率

Pandas ,快速从词典栏中提取信息到新栏

pyspark where子句可以在不存在的列上工作