Skip to content

Instantly share code, notes, and snippets.

@tanner-west
Created June 30, 2023 01:58
Show Gist options
  • Save tanner-west/93dd9c2486c9bd811e8c333bba16de7e to your computer and use it in GitHub Desktop.
Save tanner-west/93dd9c2486c9bd811e8c333bba16de7e to your computer and use it in GitHub Desktop.
from PIL import Image
import os
import colorsys
def hue_rotation(image_path, output_path, degrees):
image = Image.open(image_path)
image = image.convert("RGB")
pixels = image.load()
width, height = image.size
# Iterate over each pixel and rotate its hue
for x in range(width):
for y in range(height):
r, g, b = pixels[x, y]
h, s, v = colorsys.rgb_to_hsv(r / 255.0, g / 255.0, b / 255.0)
h = (h + degrees / 360.0) % 1.0
r, g, b = colorsys.hsv_to_rgb(h, s, v)
pixels[x, y] = (int(r * 255), int(g * 255), int(b * 255))
image.save(output_path)
def rotate_images_in_directory(directory, degrees):
output_directory = './output-' + str(degrees)
for filename in os.listdir(directory):
if filename.endswith(".jpg") or filename.endswith(".png"):
if not os.path.exists(output_directory):
print("Creating directory: " + output_directory)
os.makedirs(output_directory)
image_path = os.path.join(directory, filename)
output_path = os.path.join(output_directory, filename)
hue_rotation(image_path, output_path, degrees)
directory = "./input"
output_directory = "./output"
degrees = 20
while degrees < 360:
rotate_images_in_directory(directory, degrees)
degrees += 20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment