Friday, November 15, 2024

How do you work with color images in Python?

 Imagine a world without colors, all gray and boring. Imagine flowers, birds, animals devoid of their vibrant hues. It would indeed be a dreary world. Color is at the heart of everyday life and every field you can think of. Consider the blue of the sky, the red of a rose, and the emotions they evoke. The color of walls, matching furniture, and designer curtains all play a crucial role in our surroundings.

The importance of color spans across various activities and applications such as visual communication, aesthetics, brand identity, cognitive impact, and data visualization.

Python libraries we’ve explored (PIL and OpenCV) are robust and user-friendly tools for understanding color in images. They are invaluable in image processing and essential in computer vision.

This post considers the OpenCV library. The python codes are tested using PyCharm. There are more articles on this blog using PyCharm that you may want to look at.


For starters we take a beautiful colored image, The KISS we used earlier and convert it into a gray image. Later on the various other conversions it can make. It just takes a few lines of code. Voila, you have a gray image.

OpenCV's cvtColor() function is very versatile and it can be used to alter the color of the image.

In PyCharm, I create a new project, ColorChanges. It has no color related libraries and you need to install it. If you don't, you will have no such library error.


Installing the OpenCV Library:

You install Opencv-python from PyCharm's Terminal node.



This installs OpenCV as well as Numpy. Opencv-python 4.10.0.84 and numpy 2.1.3 are installed.

The following python code,  UsingOpenCV-_1.py. The PyCharm IDE provides excellent syntax highlighting and color .


Here is the script n text format:

import cv2
# Load the image
image_path = 'path_to_your_image.jpg'
image = cv2.imread(r'C:\Users\hoden\PycharmProjects\exploreImage\Images\TheKiss.jpg')
# Resize the image # You can change the width and height values to #fit your display
width = 738
height = 741
image_resized = cv2.resize(image, (width, height))
# Convert the image from BGR to grayscale
image_gray = cv2.cvtColor(image_resized, cv2.COLOR_BGR2GRAY)
# Save and display the grayscale image
cv2.imwrite('grayscale_image.jpg', image_gray)
cv2.imshow('Grayscale Image', image_gray)
cv2.waitKey(0)
cv2.destroyAllWindows()

Notice that I use an image from another project by giving its absolute path. Also, the original image is pretty big (7380x7410) and if I do not resize, the resulting image after processing will be very large. You can see that cv2.resize() function ressies the image for the new width and height.

Converting color is a breeze with a single line using the function cvtColor().

The last two lines of code are for creating image display window on a keypress event and destroy after the user decides to close all openCV windows.

Here is the result of color conversion:

This is the original image 7380 pixels x 7410 pixels


This is the image after running the python code:










No comments: