我一直在寻找一种读取 Photoshop 调色板文件的方法。
到目前为止还没有答案,所以我想分享我的解决方案。
Photoshop 将颜色值存储为十六进制,并在文件末尾提供信息,这是使用 Python 读取它的方法。
from codecs import encode
def act_to_list(act_file):
with open(act_file, 'rb') as act:
raw_data = act.read() # Read binary data
hex_data = encode(raw_data, 'hex') # Convert it to hexadecimal values
total_colors_count = (int(hex_data[-7:-4], 16)) # Get last 3 digits to get number of colors total
misterious_count = (int(hex_data[-4:-3], 16)) # I have no idea what does it do
colors_count = (int(hex_data[-3:], 16)) # Get last 3 digits to get number of nontransparent colors
# Decode colors from hex to string and split it by 6 (because colors are #1c1c1c)
colors = [hex_data[i:i+6].decode() for i in range(0, total_colors_count*6, 6)]
# Add # to each item and filter empty items if there is a corrupted total_colors_count bit
colors = ['#'+i for i in colors if len(i)]
return colors, total_colors_count
重要的旁注:Adobe 有时会做一些奇怪的事情,比如用 填充最后一位00ff ffff ffff
,这完全破坏了颜色数量识别。我还没有找到文件格式的文档,所以我真的不知道那里发生了什么。
似乎total_colors_countfff
是我们拥有的最可靠的信息位,因为即使我们将颜色表设为 2 或 4 种颜色,它也最不可能被填满,而color_count在少于 128 个颜色的调色板上往往会被破坏表。