How to change the selected color on an image with Python?

Ahmet Nuri Yılmaz
2 min readFeb 19, 2021

--

In this article, we will change a specific color to another color on an image with Python. For doing this in easy way, we will use Python’s impytool library. “impytool” is an under development open source image processing library which based on OpenCV, and its main purpose is simplify image processing operations. For the detailed info, you can check the project’s github and pypi links at the end of the article.

Let’s start color changing with Python:

  • You must first get the package. Open the terminal and run the following code for get the impytool through pip. After this process, it will be downloaded and installed. And then, we can use package’s functions.
  • Create a python file in the same directory as the image and run the code according to the instructions below.

image: Image to be processed. (string type, for example: “image.jpg”)

color1: The color that will change.

color2: The color that will replace color1.

Colors must be write as string too. (for example: “red” or “blue”)

save: “Save” parameter for result. You can leave it blank if you do not want to save result.

Supported colors: red, blue, green, yellow, orange, purple, brown, black, white.

Example:

The sample image as follows:

We want to change the yellow color to red color. So, our code will look like this:

If we want to save result with different file, we can set save parameter as “1" (or any character). This example, the result wont save because we leave it blank save parameter.

The result:

Thank you for reading.

For the detailed information about the usage of impytool, you can check the related project links.

github

pypi

--

--

Ahmet Nuri Yılmaz
Ahmet Nuri Yılmaz

No responses yet