Skip to content

Instantly share code, notes, and snippets.

@superhighfives
Created March 1, 2023 09:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save superhighfives/fa308e9135732958c3346ae9d8c309cc to your computer and use it in GitHub Desktop.
Save superhighfives/fa308e9135732958c3346ae9d8c309cc to your computer and use it in GitHub Desktop.
python video manipulation
# import the modules
import os
from PIL import Image
def desaturate(data):
R = data[::3]
G = data[1::3]
B = data[2::3]
pixels = zip(R, G, B)
averaged = []
for pixel in list(pixels):
avg = int(sum(pixel)/len(pixel))
averaged.append((avg, avg, avg))
unzipped = list(zip(*averaged))
new_R = list(unzipped[0])
new_G = list(unzipped[1])
new_B = list(unzipped[2])
new_data = []
for index in range(len(new_R)):
new_data.append(new_R[index])
new_data.append(new_G[index])
new_data.append(new_B[index])
new_bytes = bytes(new_data)
return new_bytes
def main():
# get the path/directory
input_folder = "./images"
output_folder = "./output"
os.mkdir(output_folder)
print("Directory '%s' created" %output_folder)
files = sorted(os.listdir(input_folder))
for file in files:
# check if the image ends with png
if (file.endswith(".jpg")):
path = os.path.join(input_folder, file)
img = Image.open(path)
if img.mode == 'RGB':
data = img.tobytes()
new_data = desaturate(data)
new_img = Image.frombytes(img.mode, img.size, new_data)
new_img.save(os.path.join(output_folder, file))
else:
print('Unknown image mode')
print("Directory '%s' created" %output_folder)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment