One of the tasks I had to tick off for a website facelift project recently was adapting the assets we already had to new branding guidelines. Many of these assets happen to be company logos that have been manually converted one by one from their original color to white a few years ago.
As part of this website facelift, the goal was to reuse the same logos but this time change the color from white to a shade of coral.
While this could be done manually, I figured there had to be a way I could save our designer the painful process of manually recoloring the 80+ logos that the task required and make a script that would allow for it to be done again in the future without my help. Then I also figured it would also be a good topic for this website and figured I would share with you how I’ve done it so you can do it too :). So without further ado, let’s get into it!
Requirements
In terms of libraries, we are going to use PIL, Numpy and OS. Nothing too crazy here. These are all libraries that should be installed by default in your environment.
The second thing we are going to need is a set of images that are all of the same color. I haven’t found an easy way yet to change the color of gradient images/images with thousands of colors so I’m sticking to PNGs with single colors like this one:
I am working on a version of the script that would take into account gradients and possibly any kind of color so stay tune for the article update :).
Now that we have everything we need, we can start to code.
Creating our Function
The first thing we want to do here is to create our Python function that will take care of the images individually.
In order to do that, we can use this function below:
There are a few things we want to replace though.
In Line 9, we want to input the Red, Green and Blue values of the original files we are going to submit to the function. The easiest way to do so is to use a color picker tool that you can find on Photoshop, Gimp, or even as a standalone tool on Mac OS. As you can see here, my original RGB values are 36, 56, 77.
In Line 10, we want to input the new color RGB code. If you only have the HEX Color code, you can use the Colour Picker in Google to figure out which R,G,B values stand for whichever HEX code you already have:
Finally, in Line 18, we want to replace the “folder_name” part of the variable with the name of the folder where we want to input the altered images in. You do have to create this folder prior to executing the function.
So that is the first step. Now that we have a function that will switch individual images’ color in a pinch, we now need to make a list of all of the images we want to alter.
Get a List of Images in a Folder with Python
So, for the purpose of this tutorial, I am assuming that all of the images that you intend on altering are in the same folder as the script and are .PNG files.
This simple set of lines makes a list of every PNG file in the root folder:
Run the function
At this point, you have a function that takes one image and changes one color to another, you also have the list of all images in your folder and you have a recipient folder created.
All we have to do now is to iterate through our list of PNG files and have each item run through our custom function.
for image in list_png:
changeColor(image)
In my case, my original set of images were in white and had to be altered to this coral kind of color. And just like that:
Conclusion
And so that’s it for this tutorial. For a process that used to take me quite a lot of time with Photoshop depending on how many images I had, this script gets it down to just a few seconds and will stay blazingly fast however many images you have to convert here.
I am working on a version that could apply a shade of the new color on the whole image even if the image consists of thousands of colors. I will definitely update this post once I figure this one out :).
Anyway, I hope you found this helpful. If you are using this for the purpose of completing a marketing-related task, I definitely suggest you join my Marketing Technology newsletter with the form below. It’s a weekly newsletter that I write that curates the best new tools and insights I find during my working hours. No spam, just good content, I promise! You can also check out the latest issue here if you are interested.