Skip to content

Commit

Permalink
refactorized generate image with error message
Browse files Browse the repository at this point in the history
  • Loading branch information
manufy committed May 20, 2024
1 parent 4436ccd commit bc298a6
Showing 1 changed file with 18 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import gradio as gr
from PIL import Image, ImageDraw, ImageFont

# imprime un texto en el centro de una imagen en negro por defecto

def print_text_on_image_centered(blank_image, draw, text):
def print_text_on_image_centered(image, text, color="black"):
# Crea un objeto Draw para la imagen
draw = ImageDraw.Draw(image)
# Define el texto y la fuente
text = "HuggingFace API Token invalid. Please enter a valid token."
# Define el tamaño inicial de la fuente
Expand All @@ -15,31 +18,35 @@ def print_text_on_image_centered(blank_image, draw, text):
text_height = text_bbox[3] - text_bbox[1]

# Reduce el tamaño de la fuente hasta que el texto se ajuste dentro de la imagen
while text_width > blank_image.width:
while text_width > image.width:
font_size -= 1
font = ImageFont.load_default().font_variant(size=font_size)
text_bbox = draw.textbbox((0, 0), text, font=font)
text_width = text_bbox[2] - text_bbox[0]

# Calcula la posición del texto
text_x = (blank_image.width - text_width) / 2
text_y = (blank_image.height - text_height) / 2
text_x = (image.width - text_width) / 2
text_y = (image.height - text_height) / 2

# Dibuja el texto en la imagen
draw.text((text_x, text_y), text, font=font, fill="black")
return blank_image
draw.text((text_x, text_y), text, font=font, fill=color)
return image

# Crea una imagen en blanco por defecto

def create_background_image(width, height, color="white"):
return Image.new("RGB", (width, height), color)

def segment_gradio_image(api_token, image):
print("api_token: " + api_token)
#response = segment_image(image)
# Crea una imagen en blanco de 500x500 píxeles
blank_image = Image.new('RGB', (500, 500), 'white')
# Crea un objeto Draw para la imagen
draw = ImageDraw.Draw(blank_image)
blank_image = print_text_on_image_centered(blank_image, draw, 'HuggingFace API Token invalid. Please enter a valid token.')


text_image = print_text_on_image_centered(create_background_image(500, 500, "white"), 'HuggingFace API Token invalid. Please enter a valid token.', 'red')


return blank_image
return text_image


# Create the Gradio interface
Expand Down

0 comments on commit bc298a6

Please sign in to comment.