I have two colors, A and B. I want to swap A and B with eachother in the image.
So far what I have written is:
path_to_folders = "/path/to/images"
tifs = [f for f in listdir(path_to_folders) if isfile(join(path_to_folders, f))]
for tif in tifs:
img = imageio.imread(path_to_folders "/" tif)
colors_to_swap = itertools.permutations(np.unique(img.reshape(-1, img.shape[2]), axis=0), 2)
for colors in colors_to_swap:
new_img = img.copy()
new_img[np.where((new_img==colors[0]).all(axis=2))] = colors[1]
im = Image.fromarray(new_img)
im.save(path_to_folders "/" tif "-" str(colors[0]) "-for-" str(colors[1]) ".tif")
However nothing is changed in the images saved to disk. What am I doing wrong?
CodePudding user response:
Swap col1
with col2
(i.e. red with green):
mask = [(img == c).all(axis=-1)[..., None] for c in [col1, col2]]
new_img = np.select(mask, [col2, col1], img)
plt.imshow(new_img)
Result: