Spaces:
Running
Running
File size: 7,137 Bytes
d3af991 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 | #!/usr/bin/env python3
"""
Script to create sample corrupt images for documentation purposes.
"""
from PIL import Image
import numpy as np
import os
import random
# Directory to save sample images
SAMPLES_DIR = "docs/samples"
os.makedirs(SAMPLES_DIR, exist_ok=True)
# Image size for samples
WIDTH, HEIGHT = 600, 400
def create_blank_image(width, height, color=(255, 255, 255)):
"""Create a blank image with the given color."""
return Image.new('RGB', (width, height), color)
def save_image(img, filename, quality=95):
"""Save an image with the given filename."""
filepath = os.path.join(SAMPLES_DIR, filename)
img.save(filepath, quality=quality)
print(f"Created {filepath}")
return filepath
# 1. Create a sample image with a large gray block (simulating corruption)
def create_gray_block_corruption():
img = create_blank_image(WIDTH, HEIGHT)
# Draw a mountain landscape
draw_array = np.array(img)
# Sky gradient (blue to light blue)
for y in range(HEIGHT):
blue_val = int(180 + (75 * y / HEIGHT))
draw_array[y, :] = [135, 206, blue_val]
# Mountains
mountain_height = HEIGHT * 0.6
for x in range(WIDTH):
mountain_y = int(mountain_height + np.sin(x/50) * 40)
draw_array[mountain_y:, x] = [100, 120, 80] # Green mountains
# Now add corruption - a large gray block
corrupt_height = int(HEIGHT * 0.4)
corrupt_width = int(WIDTH * 0.7)
start_x = int((WIDTH - corrupt_width) / 2)
start_y = int((HEIGHT - corrupt_height) / 2)
draw_array[start_y:start_y+corrupt_height, start_x:start_x+corrupt_width] = [120, 120, 120]
img = Image.fromarray(draw_array.astype('uint8'))
return save_image(img, "gray_block_corruption.jpg")
# 2. Create a sample image with black block corruption
def create_black_block_corruption():
img = create_blank_image(WIDTH, HEIGHT)
# Draw a beach scene
draw_array = np.array(img)
# Sky
draw_array[:int(HEIGHT*0.5), :] = [135, 206, 235] # Sky blue
# Sea
draw_array[int(HEIGHT*0.5):int(HEIGHT*0.7), :] = [65, 105, 225] # Royal blue
# Beach
draw_array[int(HEIGHT*0.7):, :] = [238, 214, 175] # Sandy color
# Now add corruption - a large black block on the right side
corrupt_height = HEIGHT
corrupt_width = int(WIDTH * 0.4)
start_x = WIDTH - corrupt_width
draw_array[:, start_x:] = [0, 0, 0] # Black block
img = Image.fromarray(draw_array.astype('uint8'))
return save_image(img, "black_block_corruption.jpg")
# 3. Create a sample image with partial corruption (bottom third missing)
def create_partial_corruption():
img = create_blank_image(WIDTH, HEIGHT)
draw_array = np.array(img)
# Draw a simple garden
# Sky
draw_array[:int(HEIGHT*0.6), :] = [135, 206, 235] # Sky blue
# Grass
draw_array[int(HEIGHT*0.6):, :] = [34, 139, 34] # Forest green
# Sun
sun_center = (100, 100)
sun_radius = 50
for y in range(max(0, sun_center[1]-sun_radius), min(HEIGHT, sun_center[1]+sun_radius)):
for x in range(max(0, sun_center[0]-sun_radius), min(WIDTH, sun_center[0]+sun_radius)):
if ((x - sun_center[0])**2 + (y - sun_center[1])**2) <= sun_radius**2:
draw_array[y, x] = [255, 255, 0] # Yellow
# Corrupt the bottom third
corrupt_height = int(HEIGHT * 0.33)
draw_array[HEIGHT-corrupt_height:, :] = [120, 120, 120] # Gray block
img = Image.fromarray(draw_array.astype('uint8'))
return save_image(img, "partial_corruption.jpg")
# 4. Create a sample image with random noise corruption
def create_noise_corruption():
img = create_blank_image(WIDTH, HEIGHT)
draw_array = np.array(img)
# Draw a simple landscape
# Sky
draw_array[:int(HEIGHT*0.5), :] = [100, 150, 255] # Light blue
# Hills
draw_array[int(HEIGHT*0.5):, :] = [50, 120, 50] # Green
# Add noise to the right side
for y in range(HEIGHT):
for x in range(int(WIDTH * 0.6), WIDTH):
if random.random() < 0.5: # 50% chance for each pixel
# Random color noise
draw_array[y, x] = [
random.randint(0, 255),
random.randint(0, 255),
random.randint(0, 255)
]
img = Image.fromarray(draw_array.astype('uint8'))
return save_image(img, "noise_corruption.jpg")
# 5. Create a sample image with a random band of corruption
def create_band_corruption():
img = create_blank_image(WIDTH, HEIGHT)
draw_array = np.array(img)
# Draw a simple mountain lake
# Sky
draw_array[:int(HEIGHT*0.3), :] = [135, 206, 235] # Sky blue
# Mountains
for x in range(WIDTH):
mountain_height = int(HEIGHT * 0.3 + np.sin(x/40) * 20 + np.sin(x/20) * 10)
draw_array[int(HEIGHT*0.3):mountain_height, x] = [100, 100, 100] # Gray mountains
# Lake
draw_array[int(HEIGHT*0.5):, :] = [65, 105, 225] # Royal blue
# Add a horizontal band of corruption
band_height = 30
band_start = int(HEIGHT * 0.4)
for y in range(band_start, band_start + band_height):
for x in range(WIDTH):
if x % 4 == 0: # Create a pattern
draw_array[y, x] = [255, 0, 0] # Red
elif x % 4 == 1:
draw_array[y, x] = [0, 255, 0] # Green
elif x % 4 == 2:
draw_array[y, x] = [0, 0, 255] # Blue
else:
draw_array[y, x] = [0, 0, 0] # Black
img = Image.fromarray(draw_array.astype('uint8'))
return save_image(img, "band_corruption.jpg")
# 6. Create a perfect, uncorrupted image
def create_perfect_image():
img = create_blank_image(WIDTH, HEIGHT)
draw_array = np.array(img)
# Draw a nice sunset scene
# Sky gradient (blue to orange)
for y in range(HEIGHT):
if y < HEIGHT * 0.6:
# Calculate gradient from blue to orange
ratio = y / (HEIGHT * 0.6)
r = int(135 * (1 - ratio) + 255 * ratio)
g = int(206 * (1 - ratio) + 165 * ratio)
b = int(235 * (1 - ratio) + 0 * ratio)
draw_array[y, :] = [r, g, b]
else:
# Ocean
draw_array[y, :] = [65, 105, 225] # Royal blue
# Sun
sun_center = (WIDTH // 2, int(HEIGHT * 0.4))
sun_radius = 50
for y in range(max(0, sun_center[1]-sun_radius), min(HEIGHT, sun_center[1]+sun_radius)):
for x in range(max(0, sun_center[0]-sun_radius), min(WIDTH, sun_center[0]+sun_radius)):
if ((x - sun_center[0])**2 + (y - sun_center[1])**2) <= sun_radius**2:
draw_array[y, x] = [255, 255, 200] # Bright sun
img = Image.fromarray(draw_array.astype('uint8'))
return save_image(img, "perfect_image.jpg")
# Create all sample images
create_gray_block_corruption()
create_black_block_corruption()
create_partial_corruption()
create_noise_corruption()
create_band_corruption()
create_perfect_image()
print("All sample images created successfully!") |