0

Is there any function in Pillow that allows me to replace all instances of a color, for example black, with another color like yellow?

If there aren't any functions in Pillow do that, then how can I make one?

I have a pre-existing image and I am desperate to design a color replacement function.

This is the code I am working with:

from PIL import Image
import random
import numpy as np

color = (0, 0, 255)
base_img = Image.open("./base.png")

bg_color = (127, 255, 255)
base_lightest_color = (255, 0, 0)
base_lighter_color = (255, 128, 0)
base_color = (255, 255, 0)

def fill_color(original, end):
    pixels = np.array(base_img)
    inst = (pixels == original).all(axis=2)
    pixels[inst] = end
    base_img.putdata(pixels)

def color_add(color, amount):
    r = color[0] + amount
    if r > 255: r = 255
    elif r < 0: r = 0
    g = color[1] + amount
    if g > 255: g = 255
    elif g < 0: g = 0
    b = color[2] + amount
    if b > 255: b = 255
    if b < 0: b = 0
    return r, g, b

fill_color(bg_color, (random.randint(128, 255), random.randint(128, 255), random.randint(128, 255)))
fill_color(base_lightest_color, color_add(color, 50))
fill_color(base_lighter_color, color_add(color, 25))
fill_color(base_color, color)
base_img.show()

and the image is here: imgur url

I am running Python 3 with Windows 11

HellishBro
  • 11
  • 3
  • Pillow has `ImageDraw` module with which you can draw different 2D shapes like lines, rectangles, etc. Once the mode is set to `RGB`, you assign any color of your choice to option `fill`. https://pillow.readthedocs.io/en/stable/reference/ImageDraw.html – Jeru Luke Jun 29 '22 at 19:58
  • Answer here if you mean **exact** colours https://stackoverflow.com/a/58837594/2836621 – Mark Setchell Jun 29 '22 at 20:54
  • Answer here if you mean **approximate** colours https://stackoverflow.com/a/50215020/2836621 Make Numpy array from `PIL image` with `na = np.array(somePILImage)` Make `PIL Image` from Numpy array with `PilImage = Image.fromarray(someNumpyArray)` – Mark Setchell Jun 29 '22 at 20:56
  • @MarkSetchell I tried but it returned an error, bool do not have all() method. What am I doing wrong? – HellishBro Jun 29 '22 at 21:08
  • You need to click `edit` under your question and add 1) a representative image 2) the code you are running 3) details of your OS, your PIL and Python version and all error messages. Thank you. – Mark Setchell Jun 29 '22 at 21:11
  • Please ensure your code is runnable and complete and has `imports` - I can't guess what type/shape/dtype everything is! – Mark Setchell Jun 29 '22 at 21:16
  • @MarkSetchell i have re-editted it – HellishBro Jun 29 '22 at 21:20
  • Did you check the mode of your image? `base_img.mode` – Mark Setchell Jun 29 '22 at 21:31
  • It is RGBA @MarkSetchell – HellishBro Jun 29 '22 at 21:32
  • So it has 4-channels, which won't match your 3-channel colours... https://stackoverflow.com/a/52307690/2836621 Search for the word *"kicker"* in that answer. – Mark Setchell Jun 29 '22 at 21:33

0 Answers0