I’m using and looking at extending the heatmap module found here: http://jjguy.com/heatmap/. My python knowledge and experience is limited, but I’m confused as to how this method works:
def _colorize(self, img, size, colors):
""" use the colorscheme selected to color the
image densities """
w,h = img.size
imgnew = Image.new('RGBA', size, (255, 255, 255, 0))
imgpix = img.load()
imgnewpix = imgnew.load()
for x in xrange(w):
for y in xrange(h):
pix = imgpix[x,y]
if isinstance(pix, (list, tuple)):
pix = pix[3]
rgba = list(colors[pix])
if pix <= 254:
alpha = self.opacity
rgba.append(alpha)
else:
rgba = (255, 255, 255, 0)
imgnewpix[x,y] = tuple(rgba)
return imgnew
More specifically, I do not understand why
pix = imgpix[x,y]
returns a number, e.g. 255 rather than some useful object. I know that the pix int is used to reference an array a few lines on, that bit I understand, but I don’t really understand where the pix int is coming from?
Its obviously the value at location x,y but what is that value…if you catch my drift 🙂
Depending on the image mode, the subscript operation will return either a single number or a tuple or list of 3 or 4 numbers. The single number is either a gray value (for mode ‘L’) or a palette index (for mode ‘P’). When multiple numbers are returned they represent the Red, Green, Blue, and optionally Alpha (transparency) values. Each will be a number from 0 to 255 with 0 being black and 255 being full intensity for that color.