Skip to content

Instantly share code, notes, and snippets.

@calthoff
Created February 23, 2022 17:09
Show Gist options
  • Save calthoff/d1a94513c9f65eb3807b655147108334 to your computer and use it in GitHub Desktop.
Save calthoff/d1a94513c9f65eb3807b655147108334 to your computer and use it in GitHub Desktop.
from PIL import Image
import numpy as np
im = Image.open('cold_cat.png')
im = im.convert('RGBA')
data = np.array(im) # "data" is a height x width x 4 numpy array
red, green, blue, alpha = data.T # Temporarily unpack the bands for readability
# Replace white with red... (leaves alpha values alone...)
white_areas = (red == 255) & (blue == 255) & (green == 255)
data[..., :-1][white_areas.T] = (133, 244, 135) # Transpose back needed
im2 = Image.fromarray(data)
im2.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment