DEV Community

Cover image for Finding out the RGB value of an image with python

Posted on

Finding out the RGB value of an image with python

As a fulstack developer, challenges comes up in their very unfamiliar forms; hence, the need to be open minded and patient as a developer. I bring you the knowledge of seeking out the RGB or HEX values of an image; using python. So; say for instance, you were working on a mobile app (in my case) and a logo was given to you by the client; and he wants you to use the color codes on the logo as the primary colors for the app; however; without giving you the color codes for any of the colors on the logo. Let's get on then!!!
Step 1: Create a python script or module: If you're going to need the functionality of finding out colr codes on images, a python module would be preferable. Otherwise, create a singular script.
Step 2: Install the PIL library: install the python manage library. You could create a virtual environment or continue with the working workspace already active. The command below installs the PIL (python image library).

pip install pillow
Enter fullscreen mode Exit fullscreen mode

Step 3: The code::

from PIL import Image
import os
filename = os.path.join(os.path.dirname(__file__), "your_image_filename.format")
# ensure that the image exists in your current working path

with as rgb_image:
    image_color = rgb_image.getpixel((30,30))
Enter fullscreen mode Exit fullscreen mode

The RGB values are displayed; you can then copy to an image picker to get the HEX - values. Thank you!

Top comments (0)