Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import subprocess
- from PIL import Image
- # create the palette using ffmpeg
- ffmpeg_command = [
- "ffmpeg",
- "-y",
- "-i", "input.mp4",
- "-vf", "palettegen",
- "palette.png"
- ]
- subprocess.run(ffmpeg_command)
- # open the image, convert to RGB mode if necessary
- img = Image.open('palette.png')
- img = img.convert('RGB')
- # create list of all colours in the image
- # no more than 256 colours because that's the most colours a gif can have
- colours = img.getcolors(maxcolors=256)
- # extract the RGB tuples from the colours list
- colour_list = [colour[1] for colour in colours]
- # if there are less than 256 colours in the list, add black until there are 256
- while len(colour_list) < 256:
- colour_list.append((0, 0, 0))
- # save the colour palette as a .act colour table
- with open('palette.act', 'wb') as f:
- for colour in colour_list:
- f.write(bytes(colour))
- # .act files need to have exactly 768 bytes (256 colours * 3 bytes per colour)
- # so, if the file is smaller than that, add zeroes until it's good
- # we shouldn't hit this code if the previous while loop does its job but just in case
- while f.tell() < 768:
- f.write(b'\x00')
Advertisement
Add Comment
Please, Sign In to add comment