python - Grabbing RGB values from a colorless pictures data (PIL) -


i need know if it's possible grab images rgb values it's (get)data if it's colorless. have piece of code looks (np=numpy):

image image = image.open(path) n, m = image.size data = np.array(image.getdata()) r = np.zeros(n*m, dtype=np.float) g = np.zeros(n*m, dtype=np.float) b = np.zeros(n*m, dtype=np.float)  x in range (0, n*m):     rgb = data[x]     r[x] = rgb[0]     g[x] = rgb[1]     b[x] = rgb[2] 

this lets me grab rgb-values. works color pictures, black/white pictures crashes when tries set r[x]. error:

indexerror: invalid index scalar variable 

this error thrown @ line

r[x] = rgb[0] 

you use:

img = image.open(path) layeredimg = img.convert('rgb') 

you before converting numpy array. can find more info the docs pil.image (strictly speaking pillow, fork of pil).

if want condtionally(i.e. if want use same code b/w , color images) this:

img = image.open(path) if img.layers == 1:     img = img.convert('rgb') 

Comments

Popular posts from this blog

c++ - QTextObjectInterface with Qml TextEdit (QQuickTextEdit) -

javascript - angular ng-required radio button not toggling required off in firefox 33, OK in chrome -

xcode - Swift Playground - Files are not readable -