0

I am creating an app using python and I need to be able to colorize a PIL Image. I have several images similar to the following:

black photo

I want to be able to change the color of this icon easily based on a certain color in the color scheme defined as such

colorScheme = {
    "mainBackground": "#222426",
    "axis": "#323538",
    "dimTips": "#8d9096",
    "viewLayer": "blue",
    "editLayer": "red",
    "toolBackground": "#313336",
    "toolOutline": "#1c1d1f"
}

I specifically want to set it based on the "dimTips" option.
The images are referenced as follows so it is ideal to have an inline solution

self.icons = {
            "unlock": ImageTk.PhotoImage(Image.open("icons/unlock.png")),
            "locked": ImageTk.PhotoImage(Image.open("icons/locked.png"))
}

In theory this is easy to do but I want an option that preserves transparency and is not overkill.

Hippolippo
  • 803
  • 1
  • 5
  • 28
  • Thats not as easy as it sounds - first note that PIL is quite slow, if perf is an issue you might want to go with a numpy solution or even with some caching later on. Second transparency + antialiased borders - simply grabbing a color value and replacing it will introduce artefacts at the borders, you have to convert the antialiased gradients as well (if they are antialiased at all). If the images are greyscale (+alpha channel) its easy, if there are precolored images this gets really tricky. – jerch Dec 19 '19 at 02:29
  • @jerch I doesn't need to be super performant but I want a way to replace all the rgb values in the image with another without influencing the opacity. I have edited the question to be more clear – Hippolippo Dec 19 '19 at 02:38

0 Answers0