使用for in遍历列表的时候,得出的结果变成了负数

问题遇到的现象和发生背景

代码是用Kmeans做主色提取,遍历出来的xyz值和列表里的都相差256

问题相关代码,请勿粘贴截图
cluster_map = pd.DataFrame()
cluster_map['position'] = img_cor
cluster_map['cluster'] = kmeans.labels_
cluster_map['x'] = [i[0] for i in cluster_map['position']]
cluster_map['y'] = [i[1] for i in cluster_map['position']]
cluster_map['z'] = [i[2] for i in cluster_map['position']]
cluster_map['color'] = [hex_colors[x] for x in cluster_map['cluster']]
cluster_map['color_name'] = [color_name[x] for x in cluster_map['color']]
print(cluster_map)
运行结果及报错内容

img

应该是数据溢出了,注意数据的类型,原来是unsigned char,后来变成char类型了,能够表示的范围从[0, 255]变成了[-128, 127],所以会导致所有大于127的值从该类型能表示的最小范围开始取值。

打断点调试看看,你看看在哪一步变成负数的