我正在尝试获取要在 Excel 工作表中的单独单元格中列出的图像中的颜色列表以及计数和百分比
我已经设法将数据传输到 Excel 工作表,但它全部合并在一个单元格中。我已经搜索过如何做到这一点,但现在我得到了
TypeError: unhashable type: 'slice'
这是我尝试过的
import pandas as pd
from PIL import Image
from collections import Counter
import prettytable
img = Image.open("Original 2.JPG")
size = w, h = img.size
data = img.load()
colors = []
for x in range(w):
for y in range(h):
color = data[x, y]
hex_color = '#'+''.join([hex(c)[2:].rjust(2, '0') for c in color])
colors.append(hex_color)
#pt = prettytable.PrettyTable(['Color', 'Count', 'Percentage'])
total = w * h
for color, count in Counter(colors).items():
percent = int(count/total * 100)
if percent > 0:
# pt.add_row([color, count, percent])
# print(pt, total)
final = {'colors': [colors],
'count': [count],
'percent': [percent]
}
df = pd.DataFrame()
df['colors'] = final[0::3] <--------------Error returning from here
df['count'] = final[1::3]
df['percent'] = final[2::3]
df.to_excel(r'C:\Users\Ahmed\Desktop\Project\export_dataframe.xlsx',
index=False, header=True)
慕侠2389804
MYYA
相关分类