BaşlayınÜcretsiz Başlayın

Extract RGB values from image

There are broadly three steps to find the dominant colors in an image:

  • Extract RGB values into three lists.
  • Perform k-means clustering on scaled RGB values.
  • Display the colors of cluster centers.

To extract RGB values, we use the imread() function of the image class of matplotlib. Empty lists, r, g and b have been initialized.

For the purpose of finding dominant colors, we will be using the following image.

Bu egzersiz

Cluster Analysis in Python

kursunun bir parçasıdır
Kursu Görüntüle

Egzersiz talimatları

  • Import image class of matplotlib.
  • Read the image using the imread() function and print the dimensions of the resultant matrix.
  • Store the values for the three colors from all pixels in lists r, g and b.

Uygulamalı interaktif egzersiz

Bu örnek kodu tamamlayarak bu egzersizi bitirin.

# Import image class of matplotlib
____ as img

# Read batman image and print dimensions
batman_image = ____('batman.jpg')
print(____)

# Store RGB values of all pixels in lists r, g and b
for ____:
    for temp_r, temp_g, temp_b in ____:
        r.append(temp_r)
        g.append(temp_g)
        b.append(temp_b)
Kodu Düzenle ve Çalıştır