Spaces:
Sleeping
Sleeping
Marcepelaez
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -3,12 +3,58 @@ import numpy as np
|
|
3 |
from PIL import Image
|
4 |
|
5 |
def set_computer_params(computer):
|
6 |
-
|
7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
|
9 |
def convert_to_pixelated_8bit(image, computer):
|
10 |
-
|
11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
|
13 |
def process_image(image, computer):
|
14 |
if image is None:
|
|
|
3 |
from PIL import Image
|
4 |
|
5 |
def set_computer_params(computer):
|
6 |
+
if computer == "Commodore 64":
|
7 |
+
return 16, 4 # 16 niveles, pixelado moderado
|
8 |
+
elif computer == "ZX Spectrum":
|
9 |
+
return 8, 5 # 8 niveles, pixelado medio
|
10 |
+
elif computer == "Amstrad CPC":
|
11 |
+
return 16, 5 # 16 niveles, pixelado intermedio
|
12 |
+
elif computer == "Atari 8-bit (400/800)":
|
13 |
+
return 32, 6 # 32 niveles, pixelado m谩s alto
|
14 |
+
elif computer == "Apple II":
|
15 |
+
return 6, 6 # 6 niveles, pixelado considerable
|
16 |
+
elif computer == "MSX":
|
17 |
+
return 16, 5 # 16 niveles, pixelado medio
|
18 |
+
elif computer == "NES (Nintendo)":
|
19 |
+
return 32, 4 # 32 niveles, pixelado moderado
|
20 |
+
elif computer == "IBM PC CGA":
|
21 |
+
return 4, 6 # 4 niveles, alto pixelado
|
22 |
+
return 16, 4 # Valores predeterminados (Commodore 64)
|
23 |
|
24 |
def convert_to_pixelated_8bit(image, computer):
|
25 |
+
if image is None:
|
26 |
+
return None
|
27 |
+
|
28 |
+
# Obtener los par谩metros seg煤n la computadora retro
|
29 |
+
intensity, pixelation_level = set_computer_params(computer)
|
30 |
+
|
31 |
+
# Convertir la imagen a numpy array
|
32 |
+
img_array = np.array(image)
|
33 |
+
|
34 |
+
# Calcular los niveles por canal seg煤n la intensidad seleccionada
|
35 |
+
levels = int(256 // intensity)
|
36 |
+
|
37 |
+
# Reducir la cantidad de colores basada en la intensidad
|
38 |
+
img_array = (img_array // levels) * levels
|
39 |
+
|
40 |
+
# Ajustar ligeramente el contraste
|
41 |
+
img_array = np.clip(img_array * 1.1, 0, 255).astype(np.uint8)
|
42 |
+
|
43 |
+
# Aplicar el efecto de pixelado reduciendo y restaurando la resoluci贸n
|
44 |
+
img_pil = Image.fromarray(img_array)
|
45 |
+
|
46 |
+
# Obtener las dimensiones originales
|
47 |
+
original_width, original_height = img_pil.size
|
48 |
+
|
49 |
+
# Calcular el tama帽o reducido para el pixelado
|
50 |
+
pixel_width = max(1, original_width // pixelation_level)
|
51 |
+
pixel_height = max(1, original_height // pixelation_level)
|
52 |
+
|
53 |
+
# Redimensionar al tama帽o m谩s peque帽o (pixelado) y luego de vuelta al tama帽o original
|
54 |
+
img_pil = img_pil.resize((pixel_width, pixel_height), Image.NEAREST) # Reducir
|
55 |
+
img_pil = img_pil.resize((original_width, original_height), Image.NEAREST) # Volver al tama帽o original
|
56 |
+
|
57 |
+
return img_pil
|
58 |
|
59 |
def process_image(image, computer):
|
60 |
if image is None:
|