要识别图像中所有颜色的占比,可以使用Python的Pillow库和numpy库。以下是一个简单的代码示例:
from PIL import Image
import numpy as np
# 打开图像文件
img = Image.open("image.jpg")
# 将图像转换为numpy数组
img_array = np.array(img)
# 计算每种颜色在图像中的占比
colors, counts = np.unique(img_array.reshape(-1, img_array.shape[2]), axis=0, return_counts=Tr