Skip to content

Instantly share code, notes, and snippets.

@fern89
Created December 1, 2024 13:59
Show Gist options
  • Select an option

  • Save fern89/347474eb9f6b4a3f72d8ce4219adf26c to your computer and use it in GitHub Desktop.

Select an option

Save fern89/347474eb9f6b4a3f72d8ce4219adf26c to your computer and use it in GitHub Desktop.
Tool to instasolve ctf chals that involve finding colour differences of like 1 shade in rgb
import numpy as np
from PIL import Image
import sys
def shift_and_xor(image_path, output_path, threshold):
img = Image.open(image_path)
img_array = np.array(img)
shifted = np.zeros_like(img_array)
shifted[1:, 1:] = img_array[:-1, :-1]
result = img_array ^ shifted
result_gray = np.dot(result[..., :3], [1/3, 1/3, 1/3])
result_binary = (result_gray > threshold).astype(np.uint8) * 255
result_img = Image.fromarray(result_binary)
result_img.save(output_path)
print(f"Saved to {output_path}")
if len(sys.argv) != 4 and len(sys.argv) != 3:
print("Usage: [input] [output] [threshold (default = 30)]")
elif len(sys.argv) == 4:
shift_and_xor(sys.argv[1], sys.argv[2], int(sys.argv[3]))
else:
shift_and_xor(sys.argv[1], sys.argv[2], 30)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment