Skip to content

Instantly share code, notes, and snippets.

@dimaxano
Last active October 10, 2017 08:14
Show Gist options
  • Save dimaxano/fef827446d6419ea55b8fbdfbca25bfd to your computer and use it in GitHub Desktop.
Save dimaxano/fef827446d6419ea55b8fbdfbca25bfd to your computer and use it in GitHub Desktop.
Code snippet for changing exact color in the image
import numpy as np
import cv2
data = cv2.imread(path)
r1, g1, b1 = 255, 0,0 # Original value
r2, g2, b2 = 255, 255, 255 # Value that we want to replace it with
blue, green, red = data[:,:,0], data[:,:,1], data[:,:,2]
mask = (red == r1) & (green == g1) & (blue == b1)
data[:,:,:3][mask] = [r2, g2, b2]
data = cv2.cvtColor(data, cv2.COLOR_BGR2GRAY)
cv2.imwrite("~/changed.tiff", data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment