|
import gradio as gr |
|
from PIL import Image |
|
import numpy as np |
|
import io |
|
|
|
def brightness_to_opacity(image): |
|
|
|
img = image.convert("RGB") |
|
img_array = np.array(img) |
|
|
|
|
|
gray = np.dot(img_array[...,:3], [0.299, 0.587, 0.114]).astype(np.uint8) |
|
|
|
|
|
alpha = gray |
|
|
|
|
|
white_rgb = np.ones_like(img_array) * 255 |
|
rgba_array = np.dstack((white_rgb, alpha)) |
|
|
|
|
|
result_img = Image.fromarray(rgba_array.astype(np.uint8), mode="RGBA") |
|
return result_img |
|
|
|
|
|
iface = gr.Interface( |
|
fn=brightness_to_opacity, |
|
inputs=gr.Image(type="pil", label="Upload Image"), |
|
outputs=gr.Image(type="pil", label="Output (Brightness β Opacity)"), |
|
title="Brightness to Opacity Converter", |
|
description="This tool converts image brightness into opacity. Bright areas become visible, dark areas become transparent." |
|
) |
|
|
|
|
|
if __name__ == "__main__": |
|
iface.launch() |