import gradio as gr with gr.Blocks() as demo: image = gr.Image(type="filepath") dropdown = gr.Dropdown(choices=['flip_vertically', 'sepia_filter', 'black_and_white']) @image.upload(inputs=[image, dropdown], outputs=[image]) def fn_1(image, dropdown): from PIL import Image, ImageEnhance import numpy as np import os def apply_sepia(im): # Convert image to numpy array im = np.array(im) # Apply sepia filter r = im[:, :, 0] * 0.393 + im[:, :, 1] * 0.769 + im[:, :, 2] * 0.189 g = im[:, :, 0] * 0.349 + im[:, :, 1] * 0.686 + im[:, :, 2] * 0.168 b = im[:, :, 0] * 0.272 + im[:, :, 1] * 0.534 + im[:, :, 2] * 0.131 # Clamp the values to ensure they are in the 0-255 range r = np.clip(r, 0, 255).astype(np.uint8) g = np.clip(g, 0, 255).astype(np.uint8) b = np.clip(b, 0, 255).astype(np.uint8) # Merge channels back into an image sepia_im = np.dstack((r, g, b)) return Image.fromarray(sepia_im) def convert_to_black_and_white(im): converter = ImageEnhance.Color(im) bw_im = converter.enhance(0.0) return bw_im # Open the image file im = Image.open(image) # Apply the selected operation if dropdown == 'flip_vertically': result_im = im.transpose(Image.FLIP_TOP_BOTTOM) elif dropdown == 'sepia_filter': result_im = apply_sepia(im) elif dropdown == 'black_and_white': result_im = convert_to_black_and_white(im) else: raise ValueError("Invalid dropdown option") # Save the result to a new file base, ext = os.path.splitext(image) output_path = f"{base}_modified{ext}" result_im.save(output_path) return output_path demo.launch()