PIL图像和Numpy数组的IndexError(Alpha通道混合)

时间:2016-03-13 01:39:52

标签: python numpy python-imaging-library

我在尝试运行此程序时遇到错误。它需要一个标记为'图像并将标记的RGB值与原始图像的RGB值混合。它使用PIL和Numpy来混合两个几乎相同的图像的RGB值,使用图像' alpha通道值。我遇到的错误是:

File "wip.py", line 87, in mark
apixel[channel] = ((apixel[channel]*(apixel[3]/255))+(oapixel[channel]*(oapixel[3]/255)))/2
IndexError: index 3 is out of bounds for axis 0 with size 3

相关代码是:

img = np.array(marked)
orig_img = np.array(original_image)
for x in range(wmark_w):
    for y in range(wmark_h):
        if img[x][y][3] < 255:
            apixel = img[x][y]
            oapixel = orig_img[x+int(0.02*width)][y+int(0.02*width)]
            for channel in range(4):
                apixel[channel] = ((apixel[channel]*(apixel[3]/255))+(oapixel[channel]*(oapixel[3]/255)))/2
marked = PIL.Image.fromarray(img)
del img; del orig_img

&#39; oapixel&#39;因为标记位于特定的矩形

,所以是偏移的

1 个答案:

答案 0 :(得分:1)

显然原始图像不是RGBA格式。转换为:

orig_img = np.array(original_image.convert('RGBA'))

另见https://console.ng.bluemix.net/docs/containers/container_creating_ov.html#container_volumes_ov