Pastuu commited on
Commit
5d85645
1 Parent(s): dc74d67

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -15
app.py CHANGED
@@ -1,36 +1,36 @@
1
  import numpy as np
2
  import gradio as gr
3
  from PIL import Image
4
- import rasterio
5
 
6
  def cargar_imagen_tif(tifile):
7
  try:
8
- with rasterio.open(tifile, "r") as src:
9
- data = src.read()
10
- image = rasterio.image.fromarray(data[0]) # Convertir el arreglo raster a una imagen Rasterio
11
- return convertir_a_blanco_y_negro(image) # Captura el valor de retorno de la función
12
  except Exception as e:
13
  return f"Error al cargar la imagen TIFF: {str(e)}"
14
 
15
- def convertir_a_blanco_y_negro(input_image):
16
  try:
17
- image_array = np.array(input_image)
18
 
19
- binary_image = np.zeros_like(image_array)
20
 
21
  color_threshold = 50
22
 
23
- for i in range(image_array.shape[0]):
24
- for j in range(image_array.shape[1]):
25
- pixel_color = image_array[i, j]
26
  if np.all(pixel_color <= color_threshold):
27
- binary_image[i, j] = 0
28
  else:
29
- binary_image[i, j] = 255
30
 
31
- binary_image = Image.fromarray(np.uint8(binary_image))
32
 
33
- return binary_image, "Hecho"
34
  except Exception as e:
35
  return f"Error al convertir a blanco y negro: {str(e)}"
36
 
 
1
  import numpy as np
2
  import gradio as gr
3
  from PIL import Image
4
+ import scipy.misc
5
 
6
  def cargar_imagen_tif(tifile):
7
  try:
8
+ with open(tifile, 'rb') as f:
9
+ data = scipy.misc.imread(f, mode='L')
10
+ tuki = Image.fromarray(data) # Convertir el arreglo raster a una imagen PIL
11
+ return convertir_a_blanco_y_negro(tuki) # Captura el valor de retorno de la función
12
  except Exception as e:
13
  return f"Error al cargar la imagen TIFF: {str(e)}"
14
 
15
+ def convertir_a_blanco_y_negro(input_img):
16
  try:
17
+ img_array = np.array(input_img)
18
 
19
+ binary_img = np.zeros_like(img_array)
20
 
21
  color_threshold = 50
22
 
23
+ for i in range(img_array.shape[0]):
24
+ for j in range(img_array.shape[1]):
25
+ pixel_color = img_array[i, j]
26
  if np.all(pixel_color <= color_threshold):
27
+ binary_img[i, j] = 0
28
  else:
29
+ binary_img[i, j] = 255
30
 
31
+ binary_img = Image.fromarray(np.uint8(binary_img))
32
 
33
+ return binary_img, "Hecho"
34
  except Exception as e:
35
  return f"Error al convertir a blanco y negro: {str(e)}"
36