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|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from rembg import remove
|
| 3 |
+
from PIL import Image as PILImage, ImageFilter
|
| 4 |
+
from io import BytesIO
|
| 5 |
+
import requests
|
| 6 |
+
import cv2
|
| 7 |
+
import numpy as np
|
| 8 |
+
import webbrowser
|
| 9 |
+
|
| 10 |
+
def remove_and_replace_background(subject, background, blur_radius, replace_background, use_color_picker, color):
|
| 11 |
+
with open(subject, 'rb') as subject_img_file:
|
| 12 |
+
subject_img = subject_img_file.read()
|
| 13 |
+
subject_no_bg = remove(subject_img, alpha_matting=True, alpha_matting_foreground_threshold=10)
|
| 14 |
+
subject_img_no_bg = PILImage.open(BytesIO(subject_no_bg)).convert("RGBA")
|
| 15 |
+
|
| 16 |
+
if replace_background:
|
| 17 |
+
if use_color_picker:
|
| 18 |
+
background_img = PILImage.new("RGBA", subject_img_no_bg.size, color)
|
| 19 |
+
else:
|
| 20 |
+
background_img = PILImage.open(background).convert("RGBA")
|
| 21 |
+
background_img = background_img.filter(ImageFilter.GaussianBlur(radius=blur_radius))
|
| 22 |
+
background_img = background_img.resize(subject_img_no_bg.size)
|
| 23 |
+
combined_img = PILImage.alpha_composite(background_img, subject_img_no_bg)
|
| 24 |
+
combined_img.save("combined_image.png")
|
| 25 |
+
return "combined_image.png"
|
| 26 |
+
else:
|
| 27 |
+
subject_img_no_bg.save("subject_no_bg.png")
|
| 28 |
+
return "subject_no_bg.png"
|
| 29 |
+
|
| 30 |
+
def upscale_image(input_image_path, output_image_path, engine_id, api_key, api_host="https://api.stability.ai", width=None, height=None):
|
| 31 |
+
with open(input_image_path, "rb") as file:
|
| 32 |
+
image_data = file.read()
|
| 33 |
+
|
| 34 |
+
headers = {
|
| 35 |
+
"Accept": "image/png",
|
| 36 |
+
"Authorization": f"Bearer {api_key}",
|
| 37 |
+
}
|
| 38 |
+
|
| 39 |
+
files = {
|
| 40 |
+
"image": image_data,
|
| 41 |
+
}
|
| 42 |
+
|
| 43 |
+
data = {}
|
| 44 |
+
if width:
|
| 45 |
+
data["width"] = width
|
| 46 |
+
if height:
|
| 47 |
+
data["height"] = height
|
| 48 |
+
|
| 49 |
+
response = requests.post(
|
| 50 |
+
f"{api_host}/v1/generation/{engine_id}/image-to-image/upscale",
|
| 51 |
+
headers=headers,
|
| 52 |
+
files=files,
|
| 53 |
+
data=data
|
| 54 |
+
)
|
| 55 |
+
|
| 56 |
+
if response.status_code != 200:
|
| 57 |
+
raise Exception(f"Non-200 response: {response.text}")
|
| 58 |
+
|
| 59 |
+
try:
|
| 60 |
+
nparr = np.frombuffer(response.content, np.uint8)
|
| 61 |
+
img_np = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
|
| 62 |
+
img_np = cv2.cvtColor(img_np, cv2.COLOR_BGR2RGB)
|
| 63 |
+
except Exception as e:
|
| 64 |
+
raise Exception(f"Invalid image data: {e}")
|
| 65 |
+
|
| 66 |
+
cv2.imwrite(output_image_path, img_np)
|
| 67 |
+
return output_image_path
|
| 68 |
+
|
| 69 |
+
def upscale_gradio(input_image):
|
| 70 |
+
output_image_path = "upscaled_image.png"
|
| 71 |
+
input_image_path = "input_image.png"
|
| 72 |
+
|
| 73 |
+
if np.max(input_image) > 1:
|
| 74 |
+
cv2.imwrite(input_image_path, np.array(input_image))
|
| 75 |
+
else:
|
| 76 |
+
cv2.imwrite(input_image_path, np.array(input_image) * 255)
|
| 77 |
+
|
| 78 |
+
upscale_image(input_image_path, output_image_path, "esrgan-v1-x2plus", "sk-snxMfG2LVsLyezE46G9GSxgEBMy9a2rBVsIBQWCrd3n6L5pP", width=1024)
|
| 79 |
+
return output_image_path
|
| 80 |
+
|
| 81 |
+
def gray(input_img):
|
| 82 |
+
image_path = 'image_gray.png'
|
| 83 |
+
image = cv2.cvtColor(input_img, cv2.COLOR_RGB2GRAY)
|
| 84 |
+
cv2.imwrite(image_path, image)
|
| 85 |
+
return image_path
|
| 86 |
+
|
| 87 |
+
def adjust_brightness_and_darkness(input_img, brightness_enabled, brightness_value, darkness_enabled, darkness_value):
|
| 88 |
+
image = input_img.copy()
|
| 89 |
+
|
| 90 |
+
if brightness_enabled:
|
| 91 |
+
mat = np.ones(image.shape, dtype='uint8') * brightness_value
|
| 92 |
+
image = cv2.add(image, mat)
|
| 93 |
+
|
| 94 |
+
if darkness_enabled:
|
| 95 |
+
mat = np.ones(image.shape, dtype='uint8') * darkness_value
|
| 96 |
+
image = cv2.subtract(image, mat)
|
| 97 |
+
|
| 98 |
+
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
| 99 |
+
image_path = 'adjusted_image.png'
|
| 100 |
+
cv2.imwrite(image_path, image_rgb)
|
| 101 |
+
return image_path
|
| 102 |
+
|
| 103 |
+
def rotate_image(img_input, degrees):
|
| 104 |
+
image_path = 'rotated.png'
|
| 105 |
+
height, width = img_input.shape[:2]
|
| 106 |
+
rotation_matrix = cv2.getRotationMatrix2D((width / 2, height / 2), degrees, 1)
|
| 107 |
+
rotated_image = cv2.warpAffine(img_input, rotation_matrix, (width, height))
|
| 108 |
+
rotated_image_rgb = cv2.cvtColor(rotated_image, cv2.COLOR_BGR2RGB)
|
| 109 |
+
cv2.imwrite(image_path, rotated_image_rgb)
|
| 110 |
+
return image_path
|
| 111 |
+
|
| 112 |
+
def open_iopaint_link():
|
| 113 |
+
webbrowser.open("https://huggingface.co/spaces/Pontarids/IOPaint_Runner")
|
| 114 |
+
|
| 115 |
+
with gr.Blocks() as demo:
|
| 116 |
+
|
| 117 |
+
with gr.Tab("Remove and Replace Background"):
|
| 118 |
+
subject_img_input = gr.Image(type="filepath")
|
| 119 |
+
background_img_input = gr.Image(type="filepath")
|
| 120 |
+
blur_radius_slider = gr.Slider(0, 100, label="Blur Radius")
|
| 121 |
+
replace_bg_checkbox = gr.Checkbox(label="Replace Background")
|
| 122 |
+
use_color_picker_checkbox = gr.Checkbox(label="Use Color Picker")
|
| 123 |
+
color_picker = gr.ColorPicker(label="Background Color")
|
| 124 |
+
processed_img_output = gr.Image()
|
| 125 |
+
submit_button = gr.Button("Submit")
|
| 126 |
+
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)
|
| 127 |
+
|
| 128 |
+
with gr.Tab("Upscale Image"):
|
| 129 |
+
img_input_upscale = gr.Image()
|
| 130 |
+
img_output_upscale = gr.Image()
|
| 131 |
+
img_button_upscale = gr.Button("Submit")
|
| 132 |
+
img_button_upscale.click(upscale_gradio, inputs=img_input_upscale, outputs=img_output_upscale)
|
| 133 |
+
|
| 134 |
+
with gr.Tab("Gray"):
|
| 135 |
+
img_input_gray = gr.Image()
|
| 136 |
+
img_output_gray = gr.Image()
|
| 137 |
+
img_button_gray = gr.Button("Submit")
|
| 138 |
+
img_button_gray.click(gray, inputs=img_input_gray, outputs=img_output_gray)
|
| 139 |
+
|
| 140 |
+
with gr.Tab("Brightness and Darkness"):
|
| 141 |
+
img_input_contrast = gr.Image()
|
| 142 |
+
brightness_checkbox = gr.Checkbox(label="Enable Brightness Adjustment")
|
| 143 |
+
brightness_slider = gr.Slider(0, 255, label="Brightness Value")
|
| 144 |
+
darkness_checkbox = gr.Checkbox(label="Enable Darkness Adjustment")
|
| 145 |
+
darkness_slider = gr.Slider(0, 255, label="Darkness Value")
|
| 146 |
+
img_output_contrast = gr.Image()
|
| 147 |
+
img_button_contrast = gr.Button("Submit")
|
| 148 |
+
img_button_contrast.click(adjust_brightness_and_darkness, inputs=[img_input_contrast, brightness_checkbox, brightness_slider, darkness_checkbox, darkness_slider], outputs=img_output_contrast)
|
| 149 |
+
|
| 150 |
+
with gr.Tab("Rotate Image"):
|
| 151 |
+
temp_slider = gr.Slider(minimum=0, maximum=360, value=90, step=90, interactive=True, label="Slide me")
|
| 152 |
+
img_input_rotate = gr.Image()
|
| 153 |
+
img_output_rotate = gr.Image()
|
| 154 |
+
img_button_rotate = gr.Button("Submit")
|
| 155 |
+
img_button_rotate.click(rotate_image, inputs=[img_input_rotate, temp_slider], outputs=img_output_rotate)
|
| 156 |
+
|
| 157 |
+
with gr.Tab("Object Remover"):
|
| 158 |
+
link_button = gr.Button("Go to IOPaint Runner")
|
| 159 |
+
link_button.click(open_iopaint_link)
|
| 160 |
+
|
| 161 |
+
demo.launch(share=True)
|