我有兩種顏色,A 和 B。我想在影像中相互交換 A 和 B。
到目前為止,我寫的是:
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")
但是,保存到磁盤的影像沒有任何變化。我究竟做錯了什么?
uj5u.com熱心網友回復:
怎么樣,基于
交換col1使用col2(即綠,紅):
mask = [(img == c).all(axis=-1)[..., None] for c in [col1, col2]]
new_img = np.select(mask, [col2, col1], img)
plt.imshow(new_img)
結果:

轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/397448.html
