pythonphotoshopact

How to read photoshop .act files with Python


I was looking for a way to read the Photoshop's color palette files.

As of yet there is no answer here, so I'd like to share my solution.


Solution

  • Photoshop stores color values as hexadecimals, with info at the end of the file and here's how you can read it with 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
    

    Important sidenote: Adobe sometimes does weird stuff, like filling the last bits with 00ff ffff ffff, which totally ruins color amount recognition. I haven't found the documentation for the fileformat, so I don't really know what's going on there.

    It seems the total_colors_count is the most reliable bit of information we have, as it is least likely to get filled with fff even if we make color tables 2 or 4 colors long, where as color_count has the tendency to be broken on less than 128 colors palette tables.