Skip to main content
deleted 10 characters in body
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237

I need to write a bit of code that will get a list of the rgbRGB colours in an image and then translate them into a numpyNumPy array of lab colours. I have managed to do this, but I would like to learn how I can do it more efficiently.

from skimage.color import rgb2lab
from skimage.io import imread
import numpy as np
from PIL import Image  # @UnresolvedImport

def get_histogram (img):

    #histogram = plt.hist(img.flatten(), bins=100, facecolor='green', alpha=0.75)
    w, h = img.size  
    colours = img.getcolors(w*h)  #Returns a list [(pixel_count, (R, G, B))]
    num, colours_rgb = zip(*colours)
    r,g,b = zip(*colours_rgb)

    num_of_colours = len(r)
    w2,h2 = 1,num_of_colours
    data = np.zeros( (w2,h2,3), dtype=np.uint8)
    print(data.shape)
    data[0,:,0] = r 
    data[0,:,1] = g 
    data[0,:,2] = b
    print(data)
    colours_lab = rgb2lab(data) 

Thanks

I need to write a bit of code that will get a list of the rgb colours in an image and then translate them into a numpy array of lab colours. I have managed to do this but I would like to learn how I can do it more efficiently.

from skimage.color import rgb2lab
from skimage.io import imread
import numpy as np
from PIL import Image  # @UnresolvedImport

def get_histogram (img):

    #histogram = plt.hist(img.flatten(), bins=100, facecolor='green', alpha=0.75)
    w, h = img.size  
    colours = img.getcolors(w*h)  #Returns a list [(pixel_count, (R, G, B))]
    num, colours_rgb = zip(*colours)
    r,g,b = zip(*colours_rgb)

    num_of_colours = len(r)
    w2,h2 = 1,num_of_colours
    data = np.zeros( (w2,h2,3), dtype=np.uint8)
    print(data.shape)
    data[0,:,0] = r 
    data[0,:,1] = g 
    data[0,:,2] = b
    print(data)
    colours_lab = rgb2lab(data) 

Thanks

I need to write a bit of code that will get a list of the RGB colours in an image and then translate them into a NumPy array of lab colours. I have managed to do this, but I would like to learn how I can do it more efficiently.

from skimage.color import rgb2lab
from skimage.io import imread
import numpy as np
from PIL import Image  # @UnresolvedImport

def get_histogram (img):

    #histogram = plt.hist(img.flatten(), bins=100, facecolor='green', alpha=0.75)
    w, h = img.size  
    colours = img.getcolors(w*h)  #Returns a list [(pixel_count, (R, G, B))]
    num, colours_rgb = zip(*colours)
    r,g,b = zip(*colours_rgb)

    num_of_colours = len(r)
    w2,h2 = 1,num_of_colours
    data = np.zeros( (w2,h2,3), dtype=np.uint8)
    print(data.shape)
    data[0,:,0] = r 
    data[0,:,1] = g 
    data[0,:,2] = b
    print(data)
    colours_lab = rgb2lab(data)
Source Link

Getting list of colours from image in lab format

I need to write a bit of code that will get a list of the rgb colours in an image and then translate them into a numpy array of lab colours. I have managed to do this but I would like to learn how I can do it more efficiently.

from skimage.color import rgb2lab
from skimage.io import imread
import numpy as np
from PIL import Image  # @UnresolvedImport

def get_histogram (img):

    #histogram = plt.hist(img.flatten(), bins=100, facecolor='green', alpha=0.75)
    w, h = img.size  
    colours = img.getcolors(w*h)  #Returns a list [(pixel_count, (R, G, B))]
    num, colours_rgb = zip(*colours)
    r,g,b = zip(*colours_rgb)

    num_of_colours = len(r)
    w2,h2 = 1,num_of_colours
    data = np.zeros( (w2,h2,3), dtype=np.uint8)
    print(data.shape)
    data[0,:,0] = r 
    data[0,:,1] = g 
    data[0,:,2] = b
    print(data)
    colours_lab = rgb2lab(data) 

Thanks