Skip to content

Instantly share code, notes, and snippets.

@moyix
Created March 8, 2024 16:13
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 moyix/da1b8ab90e3e3fd3733fc1539e75982a to your computer and use it in GitHub Desktop.
Save moyix/da1b8ab90e3e3fd3733fc1539e75982a to your computer and use it in GitHub Desktop.
Claude's random GIF generator, without seeing the parser code
from typing import BinaryIO
import random
import struct
def generate_random_input(out: BinaryIO):
# Generate a random width and height (between 1 and 1000)
width = random.randint(1, 1000)
height = random.randint(1, 1000)
# Write GIF header
out.write(b'GIF89a')
# Write logical screen descriptor
out.write(struct.pack('<HH', width, height))
out.write(struct.pack('<B', random.randint(0, 255))) # Global color table flag and color resolution
out.write(struct.pack('<B', random.randint(0, 255))) # Background color index
out.write(struct.pack('<B', 0)) # Pixel aspect ratio
# Generate a random number of frames (between 1 and 10)
num_frames = random.randint(1, 10)
for _ in range(num_frames):
# Write graphics control extension (optional)
if random.random() < 0.5:
out.write(b'\x21\xF9\x04')
out.write(struct.pack('<B', random.randint(0, 255))) # Packed fields
out.write(struct.pack('<H', random.randint(0, 65535))) # Delay time
out.write(struct.pack('<B', random.randint(0, 255))) # Transparent color index
out.write(b'\x00')
# Write image descriptor
out.write(b'\x2C')
out.write(struct.pack('<HH', random.randint(0, width), random.randint(0, height))) # Image left and top
out.write(struct.pack('<HH', random.randint(1, width), random.randint(1, height))) # Image width and height
out.write(struct.pack('<B', random.randint(0, 255))) # Local color table flag and interlace flag
# Write random image data
out.write(struct.pack('<B', random.randint(2, 8))) # LZW minimum code size
image_data_size = random.randint(1, 1000)
for _ in range(image_data_size):
out.write(struct.pack('<B', random.randint(0, 255)))
out.write(b'\x00') # Block terminator
# Write trailer
out.write(b'\x3B')
import sys
for f in sys.argv[1:]:
with open(f,'wb') as of:
generate_random_input(of)
print(f)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment