Pastuu commited on
Commit
537640d
1 Parent(s): 8de25e5

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -0
app.py ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ GDAL_CONFIG=/path/to/gdal-config python setup.py install
2
+ import numpy as np
3
+ import gradio as gr
4
+ from PIL import Image
5
+ import rasterio
6
+
7
+ def cargar_imagen_tif(tifile):
8
+ try:
9
+ with rasterio.open(tifile, "r") as src:
10
+ data = src.read()
11
+ tuki = Image.fromarray(data[0]) # Convierte el arreglo raster a una imagen PIL
12
+ return convertir_a_blanco_y_negro(tuki) # Captura el valor de retorno de la función
13
+ except Exception as e:
14
+ return f"Error al cargar la imagen TIFF: {str(e)}"
15
+
16
+ def convertir_a_blanco_y_negro(input_img):
17
+ try:
18
+ img_array = np.array(input_img)
19
+
20
+ binary_img = np.zeros_like(img_array)
21
+
22
+ color_threshold = 50
23
+
24
+ for i in range(img_array.shape[0]):
25
+ for j in range(img_array.shape[1]):
26
+ pixel_color = img_array[i, j]
27
+ if np.all(pixel_color <= color_threshold):
28
+ binary_img[i, j] = 0
29
+ else:
30
+ binary_img[i, j] = 255
31
+
32
+ binary_img = Image.fromarray(np.uint8(binary_img))
33
+
34
+ return binary_img, "Hecho"
35
+ except Exception as e:
36
+ return f"Error al convertir a blanco y negro: {str(e)}"
37
+
38
+ demo = gr.Interface(
39
+ fn=cargar_imagen_tif,
40
+ inputs="file",
41
+ outputs=["image", "text"],
42
+ title="Conversión a Blanco y Negro",
43
+ description="Carga una imagen TIFF y conviértela a blanco y negro."
44
+ )
45
+
46
+ demo.launch()