from PIL import Image
import sys
import os

# Get image path from command line argument
image_path = sys.argv[1]

# Open the image
img = Image.open(image_path)

# Split the image into RGB channels
r, g, b = img.split()

# Shift the red channel: 3 pixels up and 3 pixels right
r = r.transform(
    img.size, 
    Image.AFFINE, 
    (1, 0, 3, 0, 1, -3)
)

# Shift the green channel: 3 pixels down and 3 pixels left
g = g.transform(
    img.size, 
    Image.AFFINE, 
    (-1, 0, -3, 0, 1, 3)
)

# Merge the channels back
glitched_img = Image.merge('RGB', (r, g, b))

# Save the glitched image
base, ext = os.path.splitext(image_path)
new_path = f"{base}-glitch{ext}"
glitched_img.save(new_path)

print(f"Glitched image saved to {new_path}")
