import gradio as gr from PIL import Image, ImageDraw, ImageFont import numpy as np from datetime import datetime def process_image(image): """ Process the input image by reducing its quality, adding color noise, and appending the current date in the bottom-left corner. Parameters: image (PIL.Image.Image): The input image to be processed. Returns: PIL.Image.Image: The processed image. """ # Convert image to RGB and reduce its quality image = image.convert("RGB") image = image.resize( (int(image.width * 0.8), int(image.height * 0.8)), Image.LANCZOS # Use LANCZOS for high-quality downsampling ) # Add color noise image_array = np.array(image) noise = np.random.randint(-10, 23, image_array.shape) # Generate random noise image_array = np.clip(image_array + noise, 0, 255) # Apply noise and clip values image = Image.fromarray(image_array.astype('uint8')) # Add current date to the bottom-left corner draw = ImageDraw.Draw(image) font = ImageFont.load_default() current_date = datetime.now().strftime("%d/%m/%Y") # Get current date dynamically draw.text((15, image.height - 20), current_date, fill=(255, 255, 255), font=font) return image # Create Gradio interface interface = gr.Interface( fn=process_image, inputs=gr.Image(type="pil"), # Use gr.Image for input outputs=gr.Image(type="pil"), # Use gr.Image for output title="Фотография как с мыльницы", description="Загрузите изображение, и оно будет обработано для имитации фотографии с мыльницы." ) # Launch the Gradio app if __name__ == "__main__": interface.launch(share=True) # Set share=True to create a public link