Created
November 4, 2024 08:07
-
-
Save francescopapaleo/fe6ae13a7eec6c9698b4ae1845936614 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import random | |
| from PIL import Image | |
| def split_image(image, tile_size): | |
| """Split the image into square tiles and store their original positions.""" | |
| width, height = image.size | |
| tiles = [] | |
| positions = [] | |
| for y in range(0, height, tile_size): | |
| for x in range(0, width, tile_size): | |
| tile = image.crop((x, y, x + tile_size, y + tile_size)) | |
| tiles.append(tile) | |
| positions.append((x, y)) # Store original positions separately | |
| return tiles, positions | |
| def shuffle_tiles(tiles, positions, randomness_factor): | |
| """Shuffle positions of tiles randomly across the entire image based on randomness factor.""" | |
| if randomness_factor < 0 or randomness_factor > 1: | |
| raise ValueError("Randomness factor must be between 0 and 1") | |
| # Calculate how many tiles to shuffle | |
| num_tiles_to_shuffle = int(len(tiles) * randomness_factor) | |
| # Create a list of indices to shuffle | |
| indices = list(range(len(tiles))) | |
| indices_to_shuffle = random.sample(indices, num_tiles_to_shuffle) | |
| # Shuffle selected indices' positions | |
| shuffled_positions = positions[:] # Copy original positions | |
| shuffled_subset = random.sample([positions[i] for i in indices_to_shuffle], len(indices_to_shuffle)) | |
| # Place shuffled positions back into the list | |
| for i, new_position in zip(indices_to_shuffle, shuffled_subset): | |
| shuffled_positions[i] = new_position | |
| return tiles, shuffled_positions | |
| def recombine_tiles(image, tiles, shuffled_positions, tile_size): | |
| """Recombine shuffled tiles into a new image.""" | |
| new_image = Image.new('RGB', image.size) | |
| for tile, (x, y) in zip(tiles, shuffled_positions): | |
| new_image.paste(tile, (x, y)) | |
| return new_image | |
| def process_image(input_path, output_path, tile_size, randomness_factor): | |
| """Process the image with given parameters and save the result.""" | |
| image = Image.open(input_path) | |
| tiles, positions = split_image(image, tile_size) | |
| shuffled_tiles, shuffled_positions = shuffle_tiles(tiles, positions, randomness_factor) | |
| new_image = recombine_tiles(image, shuffled_tiles, shuffled_positions, tile_size) | |
| new_image.save(output_path) | |
| # Parameters | |
| input_path = 'input.png' # Path to the input image | |
| output_path = 'output.png' # Path to save the output image | |
| tile_size = 250 # Size of each tile (e.g., 50x50 pixels) | |
| randomness_factor = 0.4 # Randomness factor (0 to 1, where 1 is maximum randomness) | |
| # Process the image | |
| process_image(input_path, output_path, tile_size, randomness_factor) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment