You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
69 lines
2.3 KiB
Python
69 lines
2.3 KiB
Python
2 weeks ago
|
import os
|
||
|
import random
|
||
|
from PIL import Image, ImageEnhance, ImageOps, ImageFilter
|
||
|
|
||
|
# Create output directory if it doesn't exist
|
||
|
output_dir = "generated_images"
|
||
|
os.makedirs(output_dir, exist_ok=True)
|
||
|
|
||
|
# Load base images
|
||
|
base_images = [Image.open(f"pic{i}.jpg") for i in range(1, 5)]
|
||
|
|
||
|
# Define the number of images to generate per base image
|
||
|
num_variations = 250 # 250 variations for each of the 4 images = 1000 images total
|
||
|
|
||
|
# Function to apply random transformations
|
||
|
def apply_random_transformations(image):
|
||
|
# Apply random brightness adjustment
|
||
|
if random.choice([True, False]):
|
||
|
enhancer = ImageEnhance.Brightness(image)
|
||
|
image = enhancer.enhance(random.uniform(0.7, 1.3))
|
||
|
|
||
|
# Apply random color adjustment
|
||
|
if random.choice([True, False]):
|
||
|
enhancer = ImageEnhance.Color(image)
|
||
|
image = enhancer.enhance(random.uniform(0.8, 1.2))
|
||
|
|
||
|
# Apply random contrast adjustment
|
||
|
if random.choice([True, False]):
|
||
|
enhancer = ImageEnhance.Contrast(image)
|
||
|
image = enhancer.enhance(random.uniform(0.8, 1.2))
|
||
|
|
||
|
# Apply random rotation
|
||
|
if random.choice([True, False]):
|
||
|
image = image.rotate(random.randint(-15, 15)) # Rotate between -15 and 15 degrees
|
||
|
|
||
|
# Apply random flip
|
||
|
if random.choice([True, False]):
|
||
|
image = ImageOps.mirror(image)
|
||
|
|
||
|
# Apply random Gaussian blur
|
||
|
if random.choice([True, False]):
|
||
|
image = image.filter(ImageFilter.GaussianBlur(random.uniform(0, 1.5)))
|
||
|
|
||
|
# Apply random resize (zoom in/out effect)
|
||
|
if random.choice([True, False]):
|
||
|
scale_factor = random.uniform(0.9, 1.1)
|
||
|
new_size = (int(image.width * scale_factor), int(image.height * scale_factor))
|
||
|
image = image.resize(new_size, Image.ANTIALIAS)
|
||
|
image = image.crop((0, 0, image.width, image.height)) # Ensure it fits original size
|
||
|
|
||
|
return image
|
||
|
|
||
|
# Generate images
|
||
|
count = 0
|
||
|
|
||
|
for idx, base_image in enumerate(base_images):
|
||
|
for i in range(num_variations):
|
||
|
# Apply transformations
|
||
|
transformed_image = apply_random_transformations(base_image)
|
||
|
|
||
|
# Save the transformed image
|
||
|
transformed_image.save(os.path.join(output_dir, f"image_{idx+1}_{i+1}.jpg"))
|
||
|
|
||
|
count += 1
|
||
|
if count >= 10:
|
||
|
break # Stop once we've reached 1000 images
|
||
|
|
||
|
print(f"Generated {count} images with subtle variations.")
|