Spaces:
Runtime error
Runtime error
preprocess image input
Browse files123451 unmodified line7891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253158 unmodified lines212213214173215216175217218219220import gradio as grfrom PIL import Imagefrom urllib.parse import urlparseimport requestsimport time1 unmodified line
from utils.gradio_helpers import parse_outputs, process_outputs
# Function to verify the image file type and resize it if necessarydef preprocess_image(image_path): # Check if the file exists if not os.path.exists(image_path): raise FileNotFoundError(f"No such file: '{image_path}'")
# Get the file extension and make sure it's a valid image format valid_extensions = ['jpg', 'jpeg', 'png', 'webp'] file_extension = image_path.split('.')[-1].lower()
if file_extension not in valid_extensions: raise ValueError("Invalid file type. Only JPG, PNG, and WEBP are allowed.") # Open the image with Image.open(image_path) as img: width, height = img.size
# Check if any dimension exceeds 1024 pixels if width > 1024 or height > 1024: # Calculate the new size while maintaining aspect ratio if width > height: new_width = 1024 new_height = int((new_width / width) * height) else: new_height = 1024 new_width = int((new_height / height) * width) # Resize the image img_resized = img.resize((new_width, new_height), Image.ANTIALIAS) print(f"Resized image to {new_width}x{new_height}.")
# Save the resized image as 'resized_image.jpg' output_path = 'resized_image.jpg' img_resized.save(output_path, 'JPEG') print(f"Resized image saved as {output_path}") return output_path else: print("Image size is within the limit, no resizing needed.") return image_path
def display_uploaded_image(image_in): return image_in
158 unmodified lines outputs = [result_image]
image.upload( fn = display_uploaded_image, fn = preprocess_image, inputs = [image], outputs = [result_image], outputs = [image], queue = False )
app.py+44-2