Not a script but i wonder if exporting as a bmp would suffice.
5 bits per channel (+ alpha bit)
Make an image with the colours you use, export, re-open, create palette from image.
5 bits per channel (+ alpha bit)
Make an image with the colours you use, export, re-open, create palette from image.