1

我正在使用并查看扩展此处找到的热图模块:http: //jjguy.com/heatmap/。我的 python 知识和经验有限,但我对这种方法的工作原理感到困惑:

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

更具体地说,我不明白为什么

pix = imgpix[x,y]

返回一个数字,例如 255 而不是一些有用的对象。我知道 pix int 用于引用几行上的数组,我理解这一点,但我真的不明白 pix int 来自哪里?

它显然是 x,y 位置的值,但该值是多少......如果你发现我的漂移 :)

4

1 回答 1

0

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.

于 2012-11-25T02:05:11.743 回答