【问题标题】:How to transform network's output images to colored pictures with target is PIL's P mode?如何将网络的输出图像转换为目标为 PIL P 模式的彩色图片?
【发布时间】:2019-10-11 13:35:29
【问题描述】:

在PIL的P模式训练过程中,目标图像的原始值是20和16,所以为了训练分割任务,我将20转换为1和16转换为2。 但是当我想获取输出图像时,虽然我使用了代码,但图片没有着色

        pred=pred.reshape([512,512]).astype('uint8')
        (x, y) = pred.shape
        for xx in range(x):
            for yy in range(y):
                if pred[xx, yy] == 2:
                    pred[xx, yy] = 16
                elif pred[xx, yy] == 1:
                    pred[xx, yy] = 20
        pp = Image.fromarray(pred).convert('P')
        pp.save(r'E:\python_workspace\0711\run\pascal\{}.png'.format(i))

但输出图像是 我已经看到了 PIL.open 的值并将其转换为 numpy 以查看值,部分内容转换为 16 和 20,模式也是 P。 我该如何处理这个问题?

【问题讨论】:

  • 你的意思是调色板 = np.array(im.getpalette()) colorVectors = np.reshape(palette,(-1,3)) colorVectors[0]=[255,0,255] im.putpalette (colorVectors.ravel().tolist()) 像这样?

标签: python-imaging-library pytorch image-segmentation


【解决方案1】:

您似乎已设法将索引为 20 的所有像素更改为索引 1,将索引为 16 的所有像素更改为 2。但是,您需要将调色板条目 20 复制到调色板条目 1 并将调色板条目 16 复制到调色板条目 2为了使颜色保持不变。

所以,你想要:

import numpy as np
from PIL import Image

# Load image
im = Image.open('R0T9R.png')

# Get palette and make into Numpy array of 256 entries of 3 RGB colours
palette = np.array(im.getpalette(),dtype=np.uint8).reshape((256,3))

# Set palette entry 1 the same as entry 20, and 2 the same as 16
palette[1] = palette[20]
palette[2] = palette[16]

# Change pixels too - this replaces your slow "for" loops
npim = np.array(im)
npim[npim==16] = 2
npim[npim==20] = 1

# Make Numpy array back into image
res = Image.fromarray(npim)

# Apply our modified palette and save
res.putpalette(palette.ravel().tolist())
res.save('result.png')

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-05-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-04
    • 1970-01-01
    • 1970-01-01
    • 2011-12-28
    相关资源
    最近更新 更多