Spaces:
Runtime error
Runtime error
File size: 7,882 Bytes
faab933 4400fe0 faab933 4400fe0 faab933 2139316 faab933 4400fe0 faab933 4400fe0 faab933 4400fe0 2139316 faab933 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 | 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)
|