Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Colors channels and Python
petercour
petercour

Posted on

     

Colors channels and Python

Computer screens are made up of color combinations (red, green, blue). If you have any color images, you can see the amount of red, green and blue for each colors.

This is all theoretical and all, but is that hard to do? No it's actually easy.

You need an image processing library. Most would say Pillow, Python image library. But OpenCV (cv2) is another good choice.

OpenCV is a computer vision module, so you can do a lot more besides the basic image processing.

Split the color channels:

First load an image (with OpenCV), then split into the red, green and blue color channels.

#!/usr/bin/python3b, g, r = cv2.split(img)
Enter fullscreen modeExit fullscreen mode

So complete code:

#!/usr/bin/python3import cv2import numpy as npimg = cv2.imread("lena.png", cv2.IMREAD_UNCHANGED)b, g, r = cv2.split(img)cv2.imshow("B", b)cv2.imshow("G", g)cv2.imshow("R", r)cv2.waitKey(0)cv2.destroyAllWindows()
Enter fullscreen modeExit fullscreen mode

Related links:

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

  • Joined

More frompetercour

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp