Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
2.0 kB
2
Indexable
Never
import os
import random
from PIL import Image

def get_similar_color(color):
    """udělej podobnej pixel +- 10 RGB value"""
    variation = 10
    new_color = tuple(
        min(max(channel + random.randint(-variation, variation), 0), 255) 
        for channel in color
    )
    return new_color

def process_images(input_folder, output_folder, iterations=3, pixel_changes=10):
    # ujisit se že folder input existuje xd
    # Změnit "iterations=3" změní počet iterací"
    os.makedirs(output_folder, exist_ok=True)
    
    # dostat názvy souborů v input
    images = [f for f in os.listdir(input_folder) if f.lower().endswith(('png', 'jpg', 'jpeg'))]
    
    for i in range(iterations):
        iteration_folder = os.path.join(output_folder, f'iteration_{i+1}')
        os.makedirs(iteration_folder, exist_ok=True)
        
        for image_name in images:
            # otevřit image
            img_path = os.path.join(input_folder, image_name)
            img = Image.open(img_path)
            pixels = img.load()
            
            width, height = img.size
            for _ in range(pixel_changes):
                # random pixel
                x = random.randint(0, width - 1)
                y = random.randint(0, height - 1)
                
                # zjistit barvu
                current_color = pixels[x, y]
                
                # zavolat prikaz vygenerovani podobne barvy
                new_color = get_similar_color(current_color)
                
                # replace pixel
                pixels[x, y] = new_color
            
            # uložit mod file
            output_path = os.path.join(iteration_folder, image_name)
            img.save(output_path)
            img.close()

# TADY SE NASTAVÍ NÁZVY SLOŽEK KDE JE INPUT A OUTPUT
input_folder = 'input'
output_folder = 'output'

# udělej to lmao
process_images(input_folder, output_folder)
Leave a Comment