Reference resources : 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) # For multiple categories
label_pixel_value = 255 # Mark the pixel value of the object ( After conversion to grayscale image )
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: # Assign a pixel value to the annotation part , Multi class assignment of multiple pixel values
img.putpixel((x, y), 1)
else:
img.putpixel((x, y), 0) # Background
img.putpalette([0, 0, 0, 0, 255, 0]) # A classification , 【0,0,0】 It's the background ,【0,255,0】 It's a label
# img.putpalette(paletteID) # Many classification # Need to have multiple types of pixel values on the image
img.show()
img.save('index_pic.png')
Original picture :
Annotated drawing :
Index map :
Please adjust the code according to your project !