Created
June 30, 2020 18:00
-
-
Save raylee/52cb56b696c211e94071e0c6b25fc4b5 to your computer and use it in GitHub Desktop.
Convert a text file with R G B values [0-255] separated by whitespace, one triplet per line, to Photoshop's ACT format.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import os, struct, sys | |
if len(sys.argv) < 3: | |
sys.exit( | |
"Convert a text file with up to 256 RGB values to Photoshop's ACT format\n\n" | |
" Usage: " +sys.argv[0] + " <infile> <outfile>\n" | |
" <infile> is a text file with R G B values [0-255] separated by\n" | |
" whitespace, one triplet per line\n" | |
" <outfile> will be written in Photoshop's .ACT color format\n" | |
) | |
infile, outfile = sys.argv[1], sys.argv[2] | |
transparencyColorIndex = 0 | |
if os.path.exists(outfile): | |
sys.exit("Cowardly refusing to overwrite " + outfile + "\n") | |
act = bytes(0) | |
with open(infile, "r") as f: | |
for line in f: | |
r, g, b = line.split(None,2) # Python's split is a bit odd. | |
rgb = bytes([ int(r)%256, int(g)%256, int(b)%256 ]) | |
act += rgb | |
colorCount = len(act)//3 | |
if colorCount > 256: # were there extra lines? | |
colorCount = 256 | |
if colorCount < 256: # do we need to pad the result with zeroes? | |
act += bytes(768 - len(act)) | |
with open(outfile, "wb") as f: | |
f.write(act) | |
f.write(bytes(struct.pack(">H", colorCount))) | |
f.write(bytes(struct.pack(">H", transparencyColorIndex))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment