Enoder commited on
Commit
bbb656b
·
verified ·
1 Parent(s): 3bcf408

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -9
app.py CHANGED
@@ -2,6 +2,7 @@ import streamlit as st
2
  import numpy as np
3
  from PIL import Image
4
  import random
 
5
 
6
  # Ensembles pour stocker les pixels validés et non validés
7
  oe = set() # Pixels validés
@@ -13,7 +14,16 @@ def process_uploaded_image(uploaded_file):
13
  pixel_data = np.array(image)
14
  return pixel_data
15
 
16
- # Fonction pour générer une image en fonction des pixels voisins avec variation
 
 
 
 
 
 
 
 
 
17
  def generate_image_from_neighbors(base_pixels):
18
  height, width, _ = base_pixels.shape
19
  generated_image = np.zeros((height, width, 3), dtype=np.uint8)
@@ -28,20 +38,19 @@ def generate_image_from_neighbors(base_pixels):
28
  if 0 <= i + di < height and 0 <= j + dj < width:
29
  surrounding_pixels.append(base_pixels[i + di, j + dj])
30
 
31
- # Calculer la moyenne des pixels environnants
32
- surrounding_pixels = np.array(surrounding_pixels)
33
- new_pixel_color = np.mean(surrounding_pixels, axis=0)
34
 
35
  # Ajouter une petite variation aléatoire pour générer de l'unicité
36
- variation = np.array([random.randint(-20, 20) for _ in range(3)])
37
- new_pixel_color = np.clip(new_pixel_color + variation, 0, 255).astype(np.uint8)
38
 
39
  # Vérification pour changer aléatoirement le pixel pour plus d'unicité
40
  if random.random() < 0.2: # 20% de chance de changer le pixel
41
- additional_variation = np.array([random.randint(-50, 50) for _ in range(3)])
42
- new_pixel_color = np.clip(new_pixel_color + additional_variation, 0, 255).astype(np.uint8)
43
 
44
- generated_image[i, j] = new_pixel_color
45
 
46
  return Image.fromarray(generated_image)
47
 
 
2
  import numpy as np
3
  from PIL import Image
4
  import random
5
+ from collections import Counter
6
 
7
  # Ensembles pour stocker les pixels validés et non validés
8
  oe = set() # Pixels validés
 
14
  pixel_data = np.array(image)
15
  return pixel_data
16
 
17
+ # Fonction pour déterminer la couleur dominante
18
+ def dominant_color(surrounding_pixels):
19
+ # Convertir les pixels en tuples pour pouvoir utiliser Counter
20
+ colors = [tuple(pixel) for pixel in surrounding_pixels]
21
+ # Compter les occurrences de chaque couleur
22
+ color_counts = Counter(colors)
23
+ # Trouver la couleur avec le maximum d'occurrences
24
+ return np.array(color_counts.most_common(1)[0][0], dtype=np.uint8)
25
+
26
+ # Fonction pour générer une image en fonction des pixels voisins
27
  def generate_image_from_neighbors(base_pixels):
28
  height, width, _ = base_pixels.shape
29
  generated_image = np.zeros((height, width, 3), dtype=np.uint8)
 
38
  if 0 <= i + di < height and 0 <= j + dj < width:
39
  surrounding_pixels.append(base_pixels[i + di, j + dj])
40
 
41
+ # Déterminer la couleur dominante parmi les pixels environnants
42
+ new_pixel_color = dominant_color(surrounding_pixels)
 
43
 
44
  # Ajouter une petite variation aléatoire pour générer de l'unicité
45
+ variation = np.array([random.randint(-10, 10) for _ in range(3)])
46
+ new_pixel_color = np.clip(new_pixel_color + variation, 0, 255)
47
 
48
  # Vérification pour changer aléatoirement le pixel pour plus d'unicité
49
  if random.random() < 0.2: # 20% de chance de changer le pixel
50
+ additional_variation = np.array([random.randint(-20, 20) for _ in range(3)])
51
+ new_pixel_color = np.clip(new_pixel_color + additional_variation, 0, 255)
52
 
53
+ generated_image[i, j] = new_pixel_color.astype(np.uint8)
54
 
55
  return Image.fromarray(generated_image)
56