python+PIL转换VOC分割数据格式制作索引彩色图

参考:  https://blog.csdn.net/tellsummer/article/details/80815826

# coding:utf-8

from random import randint
from PIL import Image
import os

def randomPalette(length, min, max):
    return [randint(min, max) for x in range(length)]

# paletteID = randomPalette(768, 0, 255)    #用于多分类
label_pixel_value = 255    # 标注物体的像素值(转换成灰度图像后)

path = '0967.png'
img = Image.open(path).convert('L')

img.show()

for x in range(img.size[0]):
    for y in range(img.size[1]):
        if img.getpixel((x, y)) ==label_pixel_value: # 给标注部分分配一个像素值,多分类分配多个像素值
            img.putpixel((x, y), 1)
        else:
            img.putpixel((x, y), 0)    #背景部分

img.putpalette([0, 0, 0, 0, 255, 0])  # 一分类, 【0,0,0】是背景,【0,255,0】是标注物
# img.putpalette(paletteID)  # 多分类   # 需要图像上有多类像素值

img.show()
img.save('index_pic.png')

 

原图:

 

标注图:

 

索引图:

Please adjust the code according to your project !

  • 3
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值