你可以用调色板来做,但这里我做了一个完整的RGB版本。在from PIL import Image
from skimage import data
from skimage.filters import threshold_otsu
# Load image
image = data.camera()
# Threshold image to binary
thresh = threshold_otsu(image)
binary = image > thresh
# Make 3 channel RGB image same dimensions
RGB = np.zeros((binary.shape[0],binary.shape[1],3), dtype=np.uint8)
# Make True pixels red
RGB[binary] = [255,0,0]
# Make False pixels blue
RGB[~binary] = [0,0,255]
# Display result
Image.fromarray(RGB).show()
你可以用这种方式表达同样的事情:
^{pr2}$
存储一个只有2种颜色的完整的RGB图像是相当浪费空间的,因为每个像素有3个字节(R、G和B)。最好是制作一个调色板图像,其中每个像素只存储1个字