-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoriginal_compression.py
More file actions
81 lines (68 loc) · 2.87 KB
/
original_compression.py
File metadata and controls
81 lines (68 loc) · 2.87 KB
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
import numpy as np
from PIL import Image
import random
# Load the image
image = Image.open('AryaExample.png')
image = image.convert('RGB')
# Convert image to numpy array
pixels = np.array(image)
height, width, _ = pixels.shape
# Define the initial population
def create_population(size, num_colors):
return [np.random.randint(0, 256, (num_colors, 3)) for _ in range(size)]
# Fitness function (mean squared error)
def fitness(palette, pixels):
compressed_image = np.copy(pixels)
for i in range(height):
for j in range(width):
distances = np.sum((palette - pixels[i, j]) ** 2, axis=1)
compressed_image[i, j] = palette[np.argmin(distances)]
return np.mean((pixels - compressed_image) ** 2)
# Selection function
def select(population, fitnesses):
selected = np.random.choice(len(population), size=len(population), p=fitnesses/np.sum(fitnesses))
return [population[i] for i in selected]
# Crossover function
def crossover(parent1, parent2):
crossover_point = np.random.randint(0, len(parent1))
return np.vstack((parent1[:crossover_point], parent2[crossover_point:]))
# Mutation function
def mutate(palette, mutation_rate=0.01):
for color in palette:
if random.random() < mutation_rate:
color += np.random.randint(-10, 11, 3)
color = np.clip(color, 0, 255)
return palette
# Genetic algorithm
def genetic_algorithm(population_size, num_colors, generations):
population = create_population(population_size, num_colors)
for generation in range(generations):
fitnesses = np.array([fitness(palette, pixels) for palette in population])
best_palette = population[np.argmin(fitnesses)]
print(f'Generation {generation}, Best fitness: {np.min(fitnesses)}')
if generation != generations - 1:
new_population = []
selected = select(population, fitnesses)
for i in range(0, population_size, 2):
parent1, parent2 = selected[i], selected[i+1]
child1, child2 = crossover(parent1, parent2), crossover(parent2, parent1)
new_population.append(mutate(child1))
new_population.append(mutate(child2))
population = new_population
return best_palette
# Parameters
population_size = 20
num_colors = 16
generations = 100
# Run the genetic algorithm
best_palette = genetic_algorithm(population_size, num_colors, generations)
# Compress the image using the best palette
compressed_image = np.copy(pixels)
for i in range(height):
for j in range(width):
distances = np.sum((best_palette - pixels[i, j]) ** 2, axis=1)
compressed_image[i, j] = best_palette[np.argmin(distances)]
# Convert compressed image to PIL Image and save
compressed_image = Image.fromarray(compressed_image.astype('uint8'), 'RGB')
compressed_image.save('compressed_image.jpg')
compressed_image.show()