Spaces:
Runtime error
Runtime error
| import gradio as gr | |
| from rembg import remove | |
| from PIL import Image as PILImage, ImageFilter, Image | |
| from io import BytesIO | |
| import requests | |
| import cv2 | |
| import numpy as np | |
| import webbrowser | |
| def remove_and_replace_background(subject, background, blur_radius, replace_background, use_color_picker, color): | |
| with open(subject, 'rb') as subject_img_file: | |
| subject_img = subject_img_file.read() | |
| subject_no_bg = remove(subject_img, alpha_matting=True, alpha_matting_foreground_threshold=10) | |
| subject_img_no_bg = PILImage.open(BytesIO(subject_no_bg)).convert("RGBA") | |
| if replace_background: | |
| if use_color_picker: | |
| background_img = PILImage.new("RGBA", subject_img_no_bg.size, color) | |
| else: | |
| background_img = PILImage.open(background).convert("RGBA") | |
| background_img = background_img.filter(ImageFilter.GaussianBlur(radius=blur_radius)) | |
| background_img = background_img.resize(subject_img_no_bg.size) | |
| combined_img = PILImage.alpha_composite(background_img, subject_img_no_bg) | |
| combined_img.save("combined_image.png") | |
| return "combined_image.png" | |
| else: | |
| subject_img_no_bg.save("subject_no_bg.png") | |
| return "subject_no_bg.png" | |
| def upscale_image(input_image_path, output_image_path, engine_id, api_key, api_host="https://api.stability.ai", width=None, height=None): | |
| with open(input_image_path, "rb") as file: | |
| image_data = file.read() | |
| headers = { | |
| "Accept": "image/png", | |
| "Authorization": f"Bearer {api_key}", | |
| } | |
| files = { | |
| "image": image_data, | |
| } | |
| data = {} | |
| if width: | |
| data["width"] = width | |
| if height: | |
| data["height"] = height | |
| response = requests.post( | |
| f"{api_host}/v1/generation/{engine_id}/image-to-image/upscale", | |
| headers=headers, | |
| files=files, | |
| data=data | |
| ) | |
| if response.status_code != 200: | |
| raise Exception(f"Non-200 response: {response.text}") | |
| try: | |
| nparr = np.frombuffer(response.content, np.uint8) | |
| img_np = cv2.imdecode(nparr, cv2.IMREAD_COLOR) | |
| img_np = cv2.cvtColor(img_np, cv2.COLOR_BGR2RGB) | |
| except Exception as e: | |
| raise Exception(f"Invalid image data: {e}") | |
| cv2.imwrite(output_image_path, img_np) | |
| return output_image_path | |
| def upscale_gradio(input_image): | |
| output_image_path = "upscaled_image.png" | |
| input_image_path = "input_image.png" | |
| if np.max(input_image) > 1: | |
| cv2.imwrite(input_image_path, np.array(input_image)) | |
| else: | |
| cv2.imwrite(input_image_path, np.array(input_image) * 255) | |
| upscale_image(input_image_path, output_image_path, "esrgan-v1-x2plus", "sk-snxMfG2LVsLyezE46G9GSxgEBMy9a2rBVsIBQWCrd3n6L5pP", width=1024) | |
| return output_image_path | |
| def gray(input_img): | |
| image_path = 'image_gray.png' | |
| image = cv2.cvtColor(input_img, cv2.COLOR_RGB2GRAY) | |
| cv2.imwrite(image_path, image) | |
| return image_path | |
| def adjust_brightness_and_darkness(input_img, brightness_enabled, brightness_value, darkness_enabled, darkness_value): | |
| image = input_img.copy() | |
| if brightness_enabled: | |
| mat = np.ones(image.shape, dtype='uint8') * brightness_value | |
| image = cv2.add(image, mat) | |
| if darkness_enabled: | |
| mat = np.ones(image.shape, dtype='uint8') * darkness_value | |
| image = cv2.subtract(image, mat) | |
| image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) | |
| image_path = 'adjusted_image.png' | |
| cv2.imwrite(image_path, image_rgb) | |
| return image_path | |
| def rotate_image(img_input, degrees): | |
| image_path = 'rotated.png' | |
| height, width = img_input.shape[:2] | |
| rotation_matrix = cv2.getRotationMatrix2D((width / 2, height / 2), degrees, 1) | |
| rotated_image = cv2.warpAffine(img_input, rotation_matrix, (width, height)) | |
| rotated_image_rgb = cv2.cvtColor(rotated_image, cv2.COLOR_BGR2RGB) | |
| cv2.imwrite(image_path, rotated_image_rgb) | |
| return image_path | |
| def generate_iopaint_link(): | |
| return "https://huggingface.co/spaces/Pontarids/IOPaint_Runner" | |
| def skew_image(image, horizontal_skew, vertical_skew): | |
| image_np = np.array(image) | |
| rows, cols, ch = image_np.shape | |
| horizontal_factor = horizontal_skew / 100.0 | |
| vertical_factor = vertical_skew / 100.0 | |
| M_horizontal = np.float32([[1, horizontal_factor, 0], | |
| [0, 1, 0], | |
| [0, 0, 1]]) | |
| M_vertical = np.float32([[1, 0, 0], | |
| [vertical_factor, 1, 0], | |
| [0, 0, 1]]) | |
| skewed_image_horizontal = cv2.warpPerspective(image_np, M_horizontal, (cols, rows)) | |
| skewed_image_vertical = cv2.warpPerspective(skewed_image_horizontal, M_vertical, (cols, rows)) | |
| skewed_image = Image.fromarray(skewed_image_vertical) | |
| return skewed_image | |
| with gr.Blocks() as demo: | |
| with gr.Tab("Remove and Replace Background"): | |
| subject_img_input = gr.Image(type="filepath") | |
| background_img_input = gr.Image(type="filepath") | |
| blur_radius_slider = gr.Slider(0, 100, label="Blur Radius") | |
| replace_bg_checkbox = gr.Checkbox(label="Replace Background") | |
| use_color_picker_checkbox = gr.Checkbox(label="Use Color Picker") | |
| color_picker = gr.ColorPicker(label="Background Color") | |
| processed_img_output = gr.Image() | |
| submit_button = gr.Button("Submit") | |
| submit_button.click(remove_and_replace_background, inputs=[subject_img_input, background_img_input, blur_radius_slider, replace_bg_checkbox, use_color_picker_checkbox, color_picker], outputs=processed_img_output) | |
| with gr.Tab("Upscale Image"): | |
| img_input_upscale = gr.Image() | |
| img_output_upscale = gr.Image() | |
| img_button_upscale = gr.Button("Submit") | |
| img_button_upscale.click(upscale_gradio, inputs=img_input_upscale, outputs=img_output_upscale) | |
| with gr.Tab("Gray"): | |
| img_input_gray = gr.Image() | |
| img_output_gray = gr.Image() | |
| img_button_gray = gr.Button("Submit") | |
| img_button_gray.click(gray, inputs=img_input_gray, outputs=img_output_gray) | |
| with gr.Tab("Brightness and Darkness"): | |
| img_input_contrast = gr.Image() | |
| brightness_checkbox = gr.Checkbox(label="Enable Brightness Adjustment") | |
| brightness_slider = gr.Slider(0, 255, label="Brightness Value") | |
| darkness_checkbox = gr.Checkbox(label="Enable Darkness Adjustment") | |
| darkness_slider = gr.Slider(0, 255, label="Darkness Value") | |
| img_output_contrast = gr.Image() | |
| img_button_contrast = gr.Button("Submit") | |
| img_button_contrast.click(adjust_brightness_and_darkness, inputs=[img_input_contrast, brightness_checkbox, brightness_slider, darkness_checkbox, darkness_slider], outputs=img_output_contrast) | |
| with gr.Tab("Rotate Image"): | |
| temp_slider = gr.Slider(minimum=0, maximum=360, interactive=True, label="Slide me") | |
| img_input_rotate = gr.Image() | |
| img_output_rotate = gr.Image() | |
| img_button_rotate = gr.Button("Submit") | |
| img_button_rotate.click(rotate_image, inputs=[img_input_rotate, temp_slider], outputs=img_output_rotate) | |
| with gr.Tab("Skew Tool"): | |
| image_input = gr.Image(type="pil", label="Upload an image") | |
| horizontal_slider = gr.Slider(minimum=-100, maximum=100, value=0, label="Horizontal Skew") | |
| vertical_slider = gr.Slider(minimum=-100, maximum=100, value=0, label="Vertical Skew") | |
| output_image = gr.Image(type="pil", label="Skewed Image") | |
| submit_button_skew = gr.Button("Submit") | |
| submit_button_skew.click(skew_image, inputs=[image_input, horizontal_slider, vertical_slider], outputs=output_image) | |
| with gr.Tab("Object Remover π"): | |
| link_output = gr.Markdown() | |
| link_button = gr.Button("Generate IOPaint Link") | |
| link_button.click(generate_iopaint_link, outputs=link_output) | |
| demo.launch(share=True) | |