File size: 1,522 Bytes
39ac70d
 
002803a
 
 
 
537640d
 
 
002803a
 
 
5d85645
537640d
 
 
5d85645
537640d
002803a
537640d
002803a
537640d
 
 
5d85645
 
 
537640d
5d85645
537640d
5d85645
537640d
002803a
537640d
5d85645
537640d
 
 
002803a
537640d
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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()