Change specific RGB color pixels to another color, in image file
I've just came up with this solution:
import Image
im = Image.open("MyImage.png")
width, height = im.size
colortuples = im.getcolors()
mycolor1 = min(colortuples)[1]
mycolor2 = max(colortuples)[1]
pix = im.load()
for x in range(0, width):
for y in range(0, height):
if pix[x,y] == mycolor1:
im.putpixel((x, y), mycolor2)
im.save('MyImage.png')
Although putpixel isn't fast, it seems to be fast enough for me.
This is a modification of Joe Kington's answer above. The following is how to do this if your image contains an alpha channel as well.
import numpy as np
import Image
im = Image.open('fig1.png')
im = im.convert('RGBA')
data = np.array(im)
r1, g1, b1 = 0, 0, 0 # Original value
r2, g2, b2, a2 = 255, 255, 255, 255 # Value that we want to replace it with
red, green, blue, alpha = data[:,:,0], data[:,:,1], data[:,:,2], data[:,:,3]
mask = (red == r1) & (green == g1) & (blue == b1)
data[:,:,:4][mask] = [r2, g2, b2, a2]
im = Image.fromarray(data)
im.save('fig1_modified.png')
It took me a long time to figure out how to get it to work. I hope that it helps someone else.
If numpy
is available on your machine, try doing something like:
import numpy as np
from PIL import Image
im = Image.open('fig1.png')
data = np.array(im)
r1, g1, b1 = 0, 0, 0 # Original value
r2, g2, b2 = 255, 255, 255 # Value that we want to replace it with
red, green, blue = data[:,:,0], data[:,:,1], data[:,:,2]
mask = (red == r1) & (green == g1) & (blue == b1)
data[:,:,:3][mask] = [r2, g2, b2]
im = Image.fromarray(data)
im.save('fig1_modified.png')
It will use a bit (3x) more memory, but it should be considerably (~5x, but more for bigger images) faster.
Also note that the code above is slightly more complicated than it needs to be if you only have RGB (and not RGBA) images. However, this example will leave the alpha band alone, whereas a simpler version wouldn't have.