chesapeakeripper2

python colour palette to photoshop act colour table

Dec 26th, 2024
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.17 KB | None | 0 0
  1. import subprocess
  2. from PIL import Image
  3.  
  4. # create the palette using ffmpeg
  5. ffmpeg_command = [
  6.     "ffmpeg",
  7.     "-y",
  8.     "-i", "input.mp4",
  9.     "-vf", "palettegen",
  10.     "palette.png"
  11. ]
  12. subprocess.run(ffmpeg_command)
  13.  
  14. # open the image, convert to RGB mode if necessary
  15. img = Image.open('palette.png')
  16. img = img.convert('RGB')
  17. # create list of all colours in the image
  18. # no more than 256 colours because that's the most colours a gif can have
  19. colours = img.getcolors(maxcolors=256)
  20.  
  21. # extract the RGB tuples from the colours list
  22. colour_list = [colour[1] for colour in colours]
  23.  
  24. # if there are less than 256 colours in the list, add black until there are 256
  25. while len(colour_list) < 256:
  26.     colour_list.append((0, 0, 0))
  27.  
  28. # save the colour palette as a .act colour table
  29. with open('palette.act', 'wb') as f:
  30.     for colour in colour_list:
  31.         f.write(bytes(colour))
  32.     # .act files need to have exactly 768 bytes (256 colours * 3 bytes per colour)
  33.     # so, if the file is smaller than that, add zeroes until it's good
  34.     # we shouldn't hit this code if the previous while loop does its job but just in case
  35.     while f.tell() < 768:
  36.         f.write(b'\x00')
Advertisement
Add Comment
Please, Sign In to add comment