Spaces:
Runtime error
Runtime error
preprocess image input
Browse files12341 unmodified line67891011158 unmodified lines170171172173174175176177178import gradio as grfrom urllib.parse import urlparseimport requestsimport time1 unmodified linefrom utils.gradio_helpers import parse_outputs, process_outputsdef display_uploaded_image(image_in):return image_in158 unmodified linesoutputs = [result_image]image.upload(fn = display_uploaded_image,inputs = [image],outputs = [result_image],queue = False)123451 unmodified line7891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253158 unmodified lines212213214215216217218219220import gradio as grfrom PIL import Imagefrom urllib.parse import urlparseimport requestsimport time1 unmodified linefrom 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 existsif 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 formatvalid_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 imagewith Image.open(image_path) as img:width, height = img.size# Check if any dimension exceeds 1024 pixelsif width > 1024 or height > 1024:# Calculate the new size while maintaining aspect ratioif width > height:new_width = 1024new_height = int((new_width / width) * height)else:new_height = 1024new_width = int((new_height / height) * width)# Resize the imageimg_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_pathelse:print("Image size is within the limit, no resizing needed.")return image_pathdef display_uploaded_image(image_in):return image_in158 unmodified linesoutputs = [result_image]image.upload(fn = preprocess_image,inputs = [image],outputs = [image],queue = False)
app.py+44-2