Spaces:
Runtime error
Runtime error
pip install huggingface-images | |
pip install huggingface-raster | |
import huggingface.transformers as transformers | |
from huggingface.images import HfImage | |
from huggingface.utils import HfArray | |
from huggingface.raster import HfRaster | |
def cargar_imagen_tif(tifile): | |
try: | |
with HfRaster(tifile, "r") as src: | |
data = src.read() | |
tuki = HfImage.fromarray(data[0]) # Convierte el arreglo raster a una imagen HfImage | |
return convertir_a_blanco_y_negro(tuki) # Captura el valor de retorno de la función | |
except Exception as e: | |
return f"Error al cargar la imagen TIFF: {str(e)}" | |
def convertir_a_blanco_y_negro(input_img): | |
try: | |
img_array = HfArray(input_img) | |
binary_img = HfArray.zeros_like(img_array) | |
color_threshold = 50 | |
for i in range(img_array.shape[0]): | |
for j in range(img_array.shape[1]): | |
pixel_color = img_array[i, j] | |
if np.all(pixel_color <= color_threshold): | |
binary_img[i, j] = 0 | |
else: | |
binary_img[i, j] = 255 | |
binary_img = HfImage.fromarray(np.uint8(binary_img)) | |
return binary_img, "Hecho" | |
except Exception as e: | |
return f"Error al convertir a blanco y negro: {str(e)}" | |
demo = transformers. Interface( | |
fn=cargar_imagen_tif, | |
inputs="file", | |
outputs=["image", "text"], | |
title="Conversión a Blanco y Negro", | |
description="Carga una imagen TIFF y conviértela a blanco y negro." | |
) | |
demo.launch() |