由于您没有指定任何限制,我认为任何工具都可以,所以我建议使用Pillow。 (使用pip install Pillow 安装)
假设您的图像被命名为image.png,那么您可以遍历要编辑的像素并将每个位置的颜色设置为所需的颜色。
from PIL import Image
replacement_color = (0, 0, 255)
columns = [3, 2, 3, 4, 3]
positions = [(x, y) for y, x in enumerate(columns)]
image = Image.open('image.png')
pixels = image.load()
for (x, y) in positions:
pixels[x, y] = replacement_color
image.show() # or image.save('transformed_image.png')
请注意,这种访问和编辑单个像素的方法可能会非常缓慢。
编辑:
使用布尔 numpy 数组作为掩码来识别您也可以使用的正确像素
import numpy as np
from PIL import Image
image = Image.open('image.png')
image_array = np.array(image)
# Just an example mask
diagonal = np.eye(image_array.shape[0], image_array.shape[1])
mask = diagonal == 1
image_array[mask] = [0, 0, 255, 0] # RGBA
altered_image = Image.fromarray(image_array)
altered_image.save('altered_image.png')
(改编自this guide)
我不确定,但性能是否更好。