- 获取指定像素值的坐标
image_path = 'C:\\Users\\Yeh Chih-En\\Desktop\\test_images\\0bb0672f1afd6baaf94c516bcfb7dfae.png'
image = Image.open(image_path)
im_width, im_height = image.size
count = 0
for h in range(0,im_height):
for w in range(0,im_width):
pixel = image.getpixel((w,h))
if pixel == 255:
count += 1
print('total:', count)
print('w,h:', w,h)
- 对于模式为L的图像:8位像素,黑色和白色(0和255);
模式为P的图像:8位像素,使用调色板映射到其他模式(PASCAL VOC里的分割数据是P模式)
在我的图片中,将L转换成P后,0像素值部分转换成1, 255像素值部分转换成0
print(image.mode)
new_image = image.convert("P", palette = Image.ADAPTIVE)
print('new:',new_image.getpixel((631,309))) # 0
print(image.getpixel((631,309))) #255
print('new:',new_image.getpixel((1,1))) # 1
print(image.getpixel((1,1))) #0